Skip to content

Instantly share code, notes, and snippets.

@semuserable
Last active January 11, 2017 22:21
Show Gist options
  • Save semuserable/0c8f8e39b94b3323c870af5e33e02ede to your computer and use it in GitHub Desktop.
Save semuserable/0c8f8e39b94b3323c870af5e33e02ede to your computer and use it in GitHub Desktop.
let apply optionF optionStr =
match optionF,optionStr with
| Some f, Some str -> Some (f str)
| _ -> None
let repeat count (str:string) = String.replicate count str
let mul x y = x * y
let optionMul = Option.map mul
let retn x = Some x // official naming for upgrading a value
let (<!>) f x = Option.map f x
let (<*>) fo xo = apply fo xo
let lift1 f x = Option.map f x // same as map!!
let lift2 f x y = f <!> x <*> y
let lift3 f x y z = f <!> x <*> y <*> z
let lift4 f x y z w = f <!> x <*> y <*> z <*> w
let resultMul = apply (Option.map mul (Some 7)) (Some 3) // ugly
let resultMul2 = mul <!> retn 7 <*> retn 3 // better
let resultMul3 = retn mul <*> retn 7 <*> retn 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment