Can I use different workflows simultaneously in F#?

前端 未结 3 1690
感动是毒
感动是毒 2021-01-25 11:19

I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way

3条回答
  •  猫巷女王i
    2021-01-25 11:26

    As stated in the previous answer, one way to combine workflows in F# (Monads in Haskell) is by using a technique called Monad Transformers.

    In F# this is really tricky, here is a project that deals with that technique.

    It's possible to write the example of the previous answer by automatically combining State and Maybe (option), using that library:

    #r @"c:\packages\FSharpPlus-1.0.0\lib\net45\FSharpPlus.dll"
    
    open FSharpPlus
    open FSharpPlus.Data
    
    // Stateful computation
    let computation =
        monad {
            let! x = get
            let! y = OptionT (result (Some 10))
            do! put (x + y)
            let! x = get
            return x
        }
    
    printfn "Result: %A" (State.eval (OptionT.run computation) 1)
    

    So this is the other alternative, instead of creating your custom workflow, use a generic workflow that will be automatically inferred (a-la Haskell).

提交回复
热议问题