Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active February 4, 2020 14:53
Show Gist options
  • Save Szer/44faa432ca28f166f534fc362137d364 to your computer and use it in GitHub Desktop.
Save Szer/44faa432ca28f166f534fc362137d364 to your computer and use it in GitHub Desktop.
Lazy Builder
[<AutoOpen>]
module LazyBuilder =
let inline force(x: Lazy<_>) = x.Force()
type LazyCont<'a> = LazyCont of (unit -> Lazy<'a>)
type LazyBuilder() =
member __.Return x = lazy x
member __.ReturnFrom (x: Lazy<_>) = x
member __.Bind(Lazy x, f) = f x
member __.Delay f = LazyCont f
member __.Run (LazyCont f) = Lazy.Create (f >> force)
let l = LazyBuilder()
let foo x = lazy x
let a =
l {
printfn "1"
let! b = foo 1
printfn "%A" b
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment