Skip to content

Instantly share code, notes, and snippets.

@Szer
Created April 2, 2018 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/5ae0cc5a853ec5bc88acece76c1d3076 to your computer and use it in GitHub Desktop.
Save Szer/5ae0cc5a853ec5bc88acece76c1d3076 to your computer and use it in GitHub Desktop.
Example of Hopac streams
#r @"./packages/Hopac/lib/net45/Hopac.Core.dll"
#r @"./packages/Hopac/lib/net45/Hopac.dll"
open Hopac
open Hopac.Stream
open System
let requestDetailAsync url=
async {
Console.WriteLine ("Simulating request " + url)
try
do! Async.Sleep(1000) // let's say each request takes about a second
return Ok (url + ":body...")
with :? Exception as e ->
return Error e
}
let requestMasterAsync() : Stream<_> =
[| "http://www.example.com/1"
"http://www.example.com/2"
"http://www.example.com/3"
"http://www.example.com/4"
"http://www.example.com/5"
"http://www.example.com/6"
"http://www.example.com/7"
"http://www.example.com/8"
"http://www.example.com/9"
"http://www.example.com/10" |]
|> Stream.ofSeq
|> Stream.mapPipelinedJob 3 (requestDetailAsync >> Job.fromAsync)
//prints all results asynchronously
requestMasterAsync()
|> Stream.iterFun (printfn "%A")
|> queue
//fold stream into list synchronously
let allResults : Result<string,exn> list =
requestMasterAsync()
|> Stream.foldFun (fun results cur -> cur::results ) []
|> run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment