This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- In MLFE | |
module curry_fun | |
export curried/1 | |
curried arg1 = | |
let curried2 arg2 = | |
let curried3 arg3 = | |
arg1 + arg2 + arg3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module pipe_forward_test | |
(|>) x y = y x | |
add x y = x + y | |
add_one x = add 1 x | |
res = 2 |> add_one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) "|") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |