Question

What I would like to do is have a function that I can repeatedly pass a transformation function into and receive a combined transformation, the transformation function would be of the form 'a -> 'b

i.e. rather than compose a fixed workflow like this:

let input = async{ let! transform1 = transformAB input
                   let! transform2 = transformBC transform1
                   let! transform3 = transformCD transform2 
                   return! transform3 }

I would like to be able to do this:

let combined = buildTransform(transform1).Next(transform2).Next(transform3)

So then I could simply call combined input to get the results of the workflow.

Would this be possible without hitting the value restriction, or the compiler constraining all the transformers to be the same type?

Was it helpful?

Solution

I'm not quite sure that I got your question, you need something similar to (>>) operator applied to Async?

open System

let f1 a = async { return Int32.Parse a }
let f2 a = async { return a = 10 }
let f3 a = async { return (not a).ToString() }

// async defined via workflow syntax
// string -> Async<string>
let result a = async {
    let! x1 = f1 a
    let! x2 = f2 x1
    let! x3 = f3 x2
    return x3
    }

Async.RunSynchronously (result "10")
|> printfn "%s"

let (|>>) f1 f2 arg = async {
    let! r = f1 arg
    return! f2 r
    }

// async defined with 'combine' operator
// string -> Async<string>
let combined = f1 |>> f2 |>> f3
Async.RunSynchronously (combined "10")
|> printfn "%s"    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top