Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
SteveGilham / gist:c3a54b82b2cdccd92fa7
Created April 26, 2015 13:58
Option monad definition
type OptionBuilder() =
member self.Bind(input, operation) = Option.bind operation input
member self.Delay (operation:(unit -> 'a option)) = operation ()
member self.Return input = Some input
member self.ReturnFrom (input : 'a option) = input
let option = OptionBuilder();;
@SteveGilham
SteveGilham / gist:ccc46c41505c5c31c56d
Created April 26, 2015 13:57
Option monad example
let result = option {
let! y = f x
let! z = g y
return! h z};;
@SteveGilham
SteveGilham / gist:29898a1394b565733ca4
Created April 26, 2015 13:56
Syntactic sugar form of computation expression
builder {let! pattern = expr
compound_expr }
@SteveGilham
SteveGilham / gist:e164f41f04129e111707
Last active August 29, 2015 14:19
Continuation form of computation expression
builder.Bind(expr, (fun pattern -> compound_expr))
let ReturnAsync x = async { return x }
let (>>=!) m f = async { return Async.RunSynchronously(f(Async.RunSynchronously m)) }
let (>>!) m1 m2 = m1 >>=! fun () -> m2
let (>=>!) f g = fun x -> ((f x) >>=! g)
let ApplyAsync m1 m2 =
m1 >>=! fun f ->
m2 >>=! fun x ->
ReturnAsync (f x)
let LiftAsync f = ApplyAsync (ReturnAsync f)
let SequenceAsync (l : _ list) =
let Half_Adder x y = (XOR x y, AND x y);;
let fst (a,b) = a;;
let snd (a,b) = b;;
type Point = { x: int; y: int; move: Point -> Point }
let rec makePoint vx vy =
let doMove p = makePoint (vx + p.x) (vy + p.y)
{ x = vx; y = vy; move = doMove };;
let p = (makePoint 1 2).move(makePoint 2 3);;
{p with x = 6; y = 0 };;