Is there a standard option workflow in F#?

前端 未结 3 1236
半阙折子戏
半阙折子戏 2021-01-07 17:01

Is there an option (maybe) wokflow (monad) in the standrd F# library?

I\'ve found a dozen of hand-made implementations (1, 2) of this workflow, but I don\'t really w

3条回答
  •  心在旅途
    2021-01-07 17:45

    There's no standard computation builder for options, but if you don't need things like laziness (as added in the examples you linked) the code is straightforward enough that there's no reason not to trust it (particularly given the suggestively named Option.bind function from the standard library). Here's a fairly minimal example:

    type OptionBuilder() =
        member x.Bind(v,f) = Option.bind f v
        member x.Return v = Some v
        member x.ReturnFrom o = o
        member x.Zero () = None
    
    let opt = OptionBuilder()
    

提交回复
热议问题