“Chaining” asynchronous functions in F#

后端 未结 2 1474
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 17:15

I have created a function in F# to recover historical data from Yahoo (the classic asynchronous example for F#):

let getCSV ticker dStart dEnd =
async   {
           


        
相关标签:
2条回答
  • 2020-12-19 17:53

    I would typically write the call to the function directly inside an asynchronous workflow. This is mostly a matter of style or preference - I think that code written using asynchronous workflows is generally more explicit and doesn't use higher-order functions as often (though they're still sometimes useful):

    let test=
        [ for stock in ["MSFT";"YHOO"] ->
            async { let! data = getCSV stock (DateTime(2000, 1, 1)) (DateTime(2010, 1, 1))
                    return getReturns data } ]
        |> Async.Parallel
        |> Async.RunSynchronously 
    

    This means that the workflows executed in parallel first get the data and then call getRteurns to extract the data. The entire operation is then parallelized.

    Alternatively, you could either use Joel's solution (modify the getReturns function so that it takes an asynchronous workflow and returns an asynchronous workflow) or define a function Async.map that takes an asynchronous workflow and constructs a new one that applies some function to the result.

    Using your original getReturns function, you can then write:

    let test=
        ["MSFT";"YHOO"]
        // For every stock name, generate an asynchronous workflow
        |> List.map (fun x -> getCSV x (DateTime(2000, 1, 1)) (DateTime(2010, 1, 1)))
        // For every workflow, transform it into a workflow that 
        // applies 'getReturns' to the result of the original workflow
        |> List.map (Async.map getReturns)
        // Run them all in parallel
        |> Async.Parallel
        |> Async.RunSynchronously
    

    The definition of Async.map is quite simple:

    module Async =
      let map f workflow = async {
        let! res = workflow
        return f res }
    
    0 讨论(0)
  • 2020-12-19 18:13

    If you defined your getReturns function like this...

    let getReturns (prices:Async<(DateTime * float) list>) = async {
        let! prices = prices
        return [for i in 1..(prices.Length-1) -> i]
               |> List.map (fun i ->(fst (List.nth prices i), (snd (List.nth prices i))/(snd (List.nth prices (i-1)))))
    }
    

    Then you would be able to do this:

    let test=
        ["MSFT";"YHOO"]
        |> List.map (fun x -> getCSV x (DateTime(2000, 1, 1)) (DateTime(2010, 1, 1)))
        |> List.map getReturns
        |> Async.Parallel
        |> Async.RunSynchronously
    

    You could clean it up further by changing getCSV so that ticker is the last parameter instead of the first. This allows you to partially apply the date arguments to produce a function that only requires a ticker to execute. Then you can chain that function with getReturns.

    let test =
        let getRange = getCSV (DateTime(2000, 1, 1)) (DateTime(2010, 1, 1))
        ["MSFT"; "YHOO"]
        |> List.map (getRange >> getReturns)
        |> Async.Parallel
        |> Async.RunSynchronously
    

    Edit:

    All those List.nth calls in your getReturns function make me itchy. I'd rather use pattern-matching myself. I think you could write that function like this instead:

    let getReturns2 (prices: Async<(DateTime * float) list>) = async {
        let! prices = prices
        let rec loop items output =
            match items with
            | (_, last) :: (time, current) :: rest ->
                loop rest ((time, (last / current)) :: output)
            | [ item ] ->
                List.rev (item :: output)
            | [] ->
                List.rev output
        return loop prices []
    }
    
    0 讨论(0)
提交回复
热议问题