Skip to content

Instantly share code, notes, and snippets.

@Leonidas-from-XIV
Last active October 10, 2017 12:56
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 Leonidas-from-XIV/5fc4a34a0edcee615a526e2ed2c4943c to your computer and use it in GitHub Desktop.
Save Leonidas-from-XIV/5fc4a34a0edcee615a526e2ed2c4943c to your computer and use it in GitHub Desktop.
Mapping specifications into function signatures
type _ elem =
| Int : int elem
| String : string elem
type (_, _) spec =
| [] : ('a, 'a) spec
| (::) : 'a elem * ('b, 'c) spec -> (('a -> 'b), 'c) spec
let decode_elem = function
| String -> fun x -> x
| Int -> int_of_string
let rec decode : type b c . (b, c) spec -> b -> 'd list -> c = function
| [] -> f ()
| _ :: rest -> decode rest
decode ["foo"; "32"] [String; Int] (fun str int -> Printf.sprintf "%d %s" int str)
|> print_endline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment