Can I use different workflows simultaneously in F#?

前端 未结 3 1687
感动是毒
感动是毒 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条回答
  •  生来不讨喜
    2021-01-25 11:42

    Others already gave you a direct answer to your question. However, I think that the way the question is stated leads to a solution that is not very idiomatic from the F# perspective - this might work for you as long as you are the only person working on the code, but I would recommend against doing that.

    Even with the added details, the question is still fairly general, but here are two suggestions:

    • There is nothing wrong with reasonably used mutable state in F#. For example, it is perfectly fine to create a function that generates IDs and pass it along:

      let createGenerator() = 
        let currentID = ref 0
        (fun () -> incr currentID; !currentID)
      
    • Do you really need to generate the IDs while you are building the entities? It sounds like you could just generate a list of entities without ID and then use Seq.zip to zip the final list of entities with list of IDs.

    • As for the maybe computation, are you using it to handle regular, valid states, or to handle exceptional states? (It sounds like the first, which is the right way of doing things - but if you need to handle truly exceptional states, then you might want to use ordinary .NET exceptions).

提交回复
热议问题