Skip to content

Instantly share code, notes, and snippets.

type attribute = | Attribute of string * string
type node =
| Node of string * (attribute list) * (node list)
| Text of string
let element name attrs nodes =
Node (name, attrs, nodes)
let attr k v =
@lepoetemaudit
lepoetemaudit / Nested.elm
Created November 15, 2016 19:33
Elm implementation of simple nested data processing
import Html exposing (..)
worldData =
{ people =
[ { money = 129825, name = "Alice Brown" }
, { money = 100, name = "John Smith" }
, { money = 50000000000, name = "Scrooge McDuck" }
, { money = 2870, name = "Charlie Johnson" }
, { money = 8273280, name = "Michael Smith" }
]
@lepoetemaudit
lepoetemaudit / AndThen.elm
Created November 29, 2016 16:08
Demonstrating infix vs pipe and function approach in Elm
import Html exposing (text)
(>>=) x y = Result.andThen y x
-- We need
r = Ok "hello"
|> Result.andThen (\x -> Ok " world"
|> Result.andThen (\y -> Ok (x ++ y)))
r2 = Ok "hello"
@lepoetemaudit
lepoetemaudit / pipe_forward.mlfe
Created November 30, 2016 16:33
Pipe forward in mlfe
module pipe_forward_test
(|>) x y = y x
add x y = x + y
add_one x = add 1 x
res = 2 |> add_one
@lepoetemaudit
lepoetemaudit / curry.mlfe
Last active December 1, 2016 13:03
Faked currying in mlfe
-- In MLFE
module curry_fun
export curried/1
curried arg1 =
let curried2 arg2 =
let curried3 arg3 =
arg1 + arg2 + arg3
@lepoetemaudit
lepoetemaudit / deepmerge.ex
Last active February 16, 2017 03:36
Deep (recursively) merge maps in elixir
defmodule DeepMerge do
defp _merge(key, v1, v2) when is_map(v1) do
Map.merge(v1, v2, &_merge/3)
end
defp _merge(key, v1, v2), do: v2
def merge(map1, map2) when is_map(map1) and is_map(map2) do
Map.merge map1, map2, &_merge/3
end
@lepoetemaudit
lepoetemaudit / hackney.alp
Last active March 2, 2017 17:47
Wrapping hackney in Alpaca
module hackney
export main
-- simple result ADT
type result 'a 'b = Error 'a | Ok 'b
-- define 'bind' for result
let (*>>=) a f =
match a with
@lepoetemaudit
lepoetemaudit / dril.sh
Created May 25, 2017 14:49
Alias for interactively running the last build docker image
alias dril="docker run -it `docker images | head -n 2 | tail -n 1 | awk '{print $3}'`"
@lepoetemaudit
lepoetemaudit / codegenstack.erl
Created July 19, 2017 13:33
Stack trace for lambdas in match statement fail
[{alpaca_codegen,gen_expr,
[{env,[{<<"fails">>,1},{<<"succeeds">>,1}],alpaca_bad,0,0},
{alpaca_fun,6,undefined,1,undefined,
[{alpaca_fun_version,0,
[{'Symbol',
#{'__struct__' => record,
line => 6,
name => <<"svar_1">>,
module Program
let add x y = x + y
let add10 = add 10
let explicit_add_10 x = x + 10
(* the above types as: