Created
November 20, 2018 07:49
Star
You must be signed in to star a gist
Example of a sequential Async operation in F#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Async with | |
static member Sequential workflows = | |
let rec sequential results (workflows:_ Async list) = async { | |
match workflows with | |
| [] -> | |
return results |> List.rev | |
| workflow :: workflows -> | |
let! result = workflow | |
return! sequential (result :: results) workflows } | |
sequential [] (List.ofSeq workflows) | |
let workflows = [| for x in 1 .. 100 -> async { printfn "Computing %d" x; return x } |] | |
let x = workflows |> Async.Sequential |> Async.RunSynchronously |
grishace
commented
Nov 20, 2018
•
open Hopac
let workflows = [| for x in 1 .. 100 -> job { printfn "Computing %d" x; return x } |]
workflows
|> Stream.ofSeq
|> Stream.mapJob id
|> Stream.foldFun (fun s x -> x::s) []
|> run
Or with Async interop
open Hopac
let workflows = [| for x in 1 .. 100 -> async { printfn "Computing %d" x; return x } |]
workflows
|> Stream.ofSeq
|> Stream.mapJob Job.fromAsync
|> Stream.foldFun (fun s x -> x::s) []
|> run
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment