Last active
February 4, 2020 14:53
-
-
Save Szer/44faa432ca28f166f534fc362137d364 to your computer and use it in GitHub Desktop.
Lazy Builder
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
[<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