Skip to content

Instantly share code, notes, and snippets.

@Szer
Created September 15, 2021 22:55
Show Gist options
  • Select an option

  • Save Szer/2f43d5c0217e58747a0d67a546503d33 to your computer and use it in GitHub Desktop.

Select an option

Save Szer/2f43d5c0217e58747a0d67a546503d33 to your computer and use it in GitHub Desktop.
Early return vs non-early return CE
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