Skip to content

Instantly share code, notes, and snippets.

@CarstenKoenig
Last active June 14, 2017 21:00
Show Gist options
  • Save CarstenKoenig/32701ed30577fc7128abd150c579cc01 to your computer and use it in GitHub Desktop.
Save CarstenKoenig/32701ed30577fc7128abd150c579cc01 to your computer and use it in GitHub Desktop.
type Movement =
| Left of int
| Right of int
type MovementState<'a> = Movement list -> Movement list * 'a
type MovementBuilder () =
member x.Zero () : MovementState<unit> = fun st -> (st, ())
member __.Return x : MovementState<'a> = fun st -> (st, x)
member __.Bind(m: MovementState<'a>, f) =
fun st ->
let (st',v) = m st
(f v) st'
member __.YieldFrom m = m
member this.Yield x = this.Return x
member this.Combine (a,b) = this.Bind (a, fun _ -> b)
member this.Delay f = f ()
let movement = MovementBuilder()
let left value : MovementState<unit> =
fun xs -> xs @ [Left value], ()
let right value : MovementState<unit> =
fun xs -> xs @ [Right value], ()
[]
|> movement {
yield! (left 10)
yield! (right 20)
}
|> printfn "list %A"
//prints [Left 10; Right 20]
let test () =
[]
|> movement {
yield! left 10
yield! (fun xs -> xs @ [Right 99], ())
yield! right 20
}
|> printfn "list %A"
@CarstenKoenig
Copy link
Author

it's really just a state-monad with the state constrained to Movement list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment