Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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" }
]
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 / huffman.clj
Last active November 17, 2015 11:30
Huffman string decoder in clojure
(defn- decode-huffman [nodes buffer]
(string/join (loop [byte-pos 0 bits (first buffer)
node (last nodes)
bits-remaining 8 output []]
(if (= 0 bits-remaining)
(recur (inc byte-pos) (nth buffer (inc byte-pos)) node 8 output)
(let [next-node (if (bit-test bits 7) :right :left)]
(if (= 255 (:right node))
(if (not= (:value node) "|")
@lepoetemaudit
lepoetemaudit / polishnotation.clj
Last active October 27, 2015 15:32
Macro for polish notation (prefix, i.e. not postfix RPN) in Clojure
(defmacro pln [& args]
(let [operator-count (dec (/ (count args) 2))
operators (take operator-count args)
values (drop operator-count args)
op-vals (map vector operators (next values))]
(loop [form (first values)
op-vals op-vals]
(if (not-empty op-vals)
@lepoetemaudit
lepoetemaudit / async.fs
Last active August 29, 2015 14:27
F# Async.Parallel
open FSharp.Data
open System.Threading
["http://bbc.co.uk"; "http://www.google.co.uk"]
|> List.map (fun str ->
async {
let! html = Http.AsyncRequestString(str)
return sprintf "bbc len %d" html.Length
}
)
@lepoetemaudit
lepoetemaudit / mailbox_chat.fs
Created August 14, 2015 12:54
F# mailboxes
open System.Threading
type ChatMessage =
| SendMessage of string
| GetContent of AsyncReplyChannel<string list>
let agent = MailboxProcessor.Start(fun agent ->
let rec loop (state : string list) = async {
printfn "List length is now %d" state.Length