Created
September 15, 2021 22:55
-
-
Save Szer/2f43d5c0217e58747a0d67a546503d33 to your computer and use it in GitHub Desktop.
Early return vs non-early return CE
This file contains hidden or 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 NoEarlyReturn() = | |
| member _.Return x = [x] | |
| member _.Combine (x, y) = x @ y | |
| member _.Delay f = f() | |
| let wtf = NoEarlyReturn() | |
| wtf { | |
| return 1 | |
| return 2 | |
| } | |
| |> printfn "%A" // [1,2] | |
| type EarlyReturn() = | |
| member _.Return x = Some [x] | |
| member _.Combine (x, y) = Option.orElseWith y x | |
| member _.Delay f = f | |
| member _.Zero() = None | |
| member _.Run f = Option.defaultValue [] (f()) | |
| let omg = EarlyReturn() | |
| omg { | |
| if true then | |
| return 1 | |
| return 2 | |
| return 3 | |
| } | |
| |> printfn "%A" // [1] | |
| omg { | |
| if false then | |
| return 1 | |
| return 2 | |
| return 3 | |
| } | |
| |> printfn "%A" // [2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment