Skip to content

Instantly share code, notes, and snippets.

@artur-s
Created November 12, 2016 23:23
Show Gist options
  • Save artur-s/93b2c151ff5f86ff5c857d106b49a076 to your computer and use it in GitHub Desktop.
Save artur-s/93b2c151ff5f86ff5c857d106b49a076 to your computer and use it in GitHub Desktop.
An example of Kleisli composition
// An example of Kleisli composition
let (>>=) m f = Option.bind f m
let (>=>) mf mg = fun x -> mf x >>= mg
let tryParseInt s =
match System.Int32.TryParse s with
| true, v -> Some v
| _ -> None
let name = function
| 0 -> Some "zero"
| 1 -> Some "jeden"
| 2 -> Some "dwa"
| _ -> None
let nameHead = List.tryHead >=> tryParseInt >=> name
nameHead []
nameHead ["x";"123";"789"]
nameHead ["2";"123";"789"]
nameHead ["123";"456";"789"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment