Last active
June 14, 2017 21:00
-
-
Save CarstenKoenig/32701ed30577fc7128abd150c579cc01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's really just a state-monad with the state constrained to
Movement list