Skip to content

Instantly share code, notes, and snippets.

View pragdave's full-sized avatar
🖍️
Alternating between Haskell and PDP-11 assembler

Dave Thomas pragdave

🖍️
Alternating between Haskell and PDP-11 assembler
View GitHub Profile
@pragdave
pragdave / test.ex
Created October 16, 2013 03:02
Description
defmodule Test do
def fred, do: 1
end
defmodule Macros do
defmacro test(do: body) do
quote hygiene: [vars: false] do
def meth(a,b), do: unquote(body)
end
end
end
defmodule UseMacro do
import Macros
@pragdave
pragdave / gist:6065316
Last active December 20, 2015 03:39
A simple DSL for gen_fsm. This generates regular gen_fsm functions (so, for example, each state/event combination is a separate function head)
defmodule Listen.NewFSM do
use Daves.Fsm, register: {:local, :listen_fsm},
initial_state: :start,
init_params: []
@timeout 3*1000
defrecord CallInfo, from: "", to: "", suspicious_segments: 0
@pragdave
pragdave / gist:5997018
Created July 15, 2013 01:57
Two implementations of flatten/1
# The simplest version is probably to use list concatenation. However,
# this version ends up rebuilding the list at each step
defmodule UsingConcat do
def flatten([]), do: []
def flatten([ head | tail ]), do: flatten(head) ++ flatten(tail)
def flatten(head), do: [ head ]
end
# This version is more efficient, as it picks successive head values
# from a list, adding them to `result`. The trick is that we have to
defrecord Poker.Card, face: 0, suit: "" do
def value(record) do
case record.face do
"A" -> 14
"K" -> 13
"Q" -> 12
"J" -> 11
"T" -> 10
int -> int
defmodule CheckDigit do
import Enum
@doc """
Determine if a sequence of digits is valid, assuming the last digit is
a Luhn checksum. (http://en.wikipedia.org/wiki/Luhn_algorithm)
"""
def valid?(numbers) when is_list(numbers) do
import :timer, only: [ sleep: 1 ]
defmodule Link1 do
def sad_method do
sleep 500
exit(99)
end
def run do
spawn(Link1, :sad_method, [])
defmodule A do
def c(name) do
IO.puts "In c(#{name})"
end
end
wrapper = quote do
defmodule Wrapper do
<?xml version="1.0" encoding="UTF-8"?> <!-- -*- xml -*- -->
<!DOCTYPE ref-class SYSTEM "local/xml/markup.dtd">
<ref-class name="Array" id="ref:Array" super-class="Object">
<mi:Class>Array</mi:Class>
<relies-on>each, &lt;=&gt;</relies-on>
<p>
Arrays are ordered, integer-indexed collections of any object.
Array indexes start at 0, as in C or Java. A negative index is
relative to the end of the array; that is, an index of -1
defprotocol WhizBang do
def specific(_)
def common(_)
end
defrecord Foo, name: "foo"
defrecord Bar, name: "bar"
defmodule Common do
def common(arg), do: IO.puts "Common.common: #{inspect arg}"