Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Created November 20, 2018 07:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save isaacabraham/1255f5c663176dfae9495987c4d6e460 to your computer and use it in GitHub Desktop.
Example of a sequential Async operation in F#.
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
Copy link

grishace commented Nov 20, 2018

open FSharp.Control

workflows
        |> AsyncSeq.ofSeq
        |> AsyncSeq.mapAsync id
        |> AsyncSeq.toArrayAsync
        |> Async.RunSynchronously

@Szer
Copy link

Szer commented Nov 23, 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

@Szer
Copy link

Szer commented Nov 23, 2018

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