Domanda

C'è un'opzione (forse) wokflow (Monade) nella Standrd F libreria #?

Ho trovato una dozzina di implementazioni fatti a mano ( 1 , 2 ) di questo flusso di lavoro, ma io in realtà non voglio introdurre non standard e non codice molto fiducia nel mio progetto. E tutte le query immaginabili a Google e MSDN mi ha dato alcun indizio dove trovarlo.

È stato utile?

Soluzione

There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number of useful monads.

Altri suggerimenti

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()

I've create an opensource library FSharp.Interop.NullOptAble available on nuget.

It not only works as an option workflow, but it works as a null or nullable workflow as well.

let x = Nullable(3)
let y = Nullable(3)
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some 6) *)

Works just as well as

let x = Some(3)
let y = Some(3)
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some 6) *)

Or even

let x = "Hello "
let y = "World"
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal (Some "Hello World") *)

And if something is null or None

let x = "Hello "
let y:string = null
option {
    let! x' = x
    let! y' = y
    return (x' + y')
} (* |> should equal None *)

Finally if you have a lot of nullable type things, I have a cexpr for chooseSeq {} and if you yield! something null/None it just doesn't get yielded.

See more examples here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top