fsprojects / Chessie

Railway-oriented programming for .NET
http://fsprojects.github.io/Chessie/
The Unlicense
188 stars 43 forks source link

Async Adapter functions #31

Closed TheAngryByrd closed 5 years ago

TheAngryByrd commented 8 years ago

I was wanting some adapter functions that lived in the AsyncResult space. I shoehorned these into one of the projects I'm currently working on.

        // ('a -> Result<'b,'c>) -> ('a -> AsyncResult<'b,'c>)
        let resultToAsync (f : 'a -> Result<'b,'c>) x =  x |> f |> Async.singleton |> AR 
        // ('a -> 'b) -> (AsyncResult<'a,'c> -> AsyncResult<'b,'c>)
        let liftAsyncResult (oneTrackFunction : 'a -> 'b) (twoTrackInput : AsyncResult<'a,'c>) = asyncTrial {
                let! twoTrackResult = twoTrackInput
                return oneTrackFunction twoTrackResult
            }
        // ('a -> AsyncResult<'b,'c>) -> (AsyncResult<'a,'c> -> AsyncResult<'b,'c>)
        let bindAsyncResult (switchFunction : 'a -> AsyncResult<'b,'c>) (twoTrackInput : AsyncResult<'a,'c>)= asyncTrial {
                let! twoTrackResult = twoTrackInput
                return! switchFunction twoTrackResult
            }       

The use case I had in mind was:


        let updateSomeRecord = resultToAsync validate
                                >> bindAsyncResult queryDatabase
                                >> liftAsyncResult saveResults 
                                >> Async.ofAsyncResult

Probably not the best implementation and names need to change. Should I make a PR for this?