Created
April 2, 2018 22:38
-
-
Save Szer/5ae0cc5a853ec5bc88acece76c1d3076 to your computer and use it in GitHub Desktop.
Example of Hopac streams
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
#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