Skip to content

Instantly share code, notes, and snippets.

@rapha
Created December 2, 2009 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rapha/246774 to your computer and use it in GitHub Desktop.
Save rapha/246774 to your computer and use it in GitHub Desktop.
Dictionary using closures in OCaml
let empty key = None
let put key value lookup =
(fun k -> if (k = key) then Some value else (lookup k))
(* use *)
let (|>) x f = f x
let lookup = empty |> put "a" 1 |> put "b" 2 |> put "c" 3;;
["a"; "b"; "c"; "d"] |> List.map lookup |> List.iter (function Some i -> Printf.printf "%d\n" i | None -> print_endline "None")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment