2 people like it.

Joinads example translated to Hopac

Joinads example from here https://github.com/tpetricek/FSharp.Joinads/blob/master/README.markdown translated to Hopac. No language extension needed, the code is equally compact and arguably more readable.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
open Hopac
open Hopac.Infixes
open Hopac.Alt.Infixes
open Hopac.Job.Infixes

let putString = mb()
let putInt = mb()
let get = ch()

Job.foreverServer (
    Alt.choose [ putString >>=? fun v -> get <-- sprintf "Echo %s" v
                 putInt >>=? fun v -> get <-- sprintf "Echo %d" v ])
|> run

Job.foreverServer (get |>> printfn "GOT: %s") |> run

// Put 5 values to 'putString' and 5 values to 'putInt'
job {
    for i in 1 .. 5 do
        do! putString <<-+ "Hello!"
        do! putInt <<-+ i 
} |> run
namespace Hopac
module Infixes

from Hopac
Multiple items
type Alt<'T> =
  inherit Job<'T>

Full name: Hopac.Alt<_>

--------------------
module Alt

from Hopac
Multiple items
type Job<'T> =

Full name: Hopac.Job<_>

--------------------
module Job

from Hopac
val putString : Mailbox<string>

Full name: Script.putString
val putInt : Mailbox<int>

Full name: Script.putInt
val get : Ch<string>

Full name: Script.get
type Job<'T> =

Full name: Hopac.Job<_>
val foreverServer : Job<unit> -> Job<unit>

Full name: Hopac.Job.foreverServer
type Alt<'T> =
  inherit Job<'T>

Full name: Hopac.Alt<_>
val choose : seq<#Alt<'x>> -> Alt<'x>

Full name: Hopac.Alt.choose
val v : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val v : int
val run : Job<'x> -> 'x

Full name: Hopac.TopLevel.run
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val job : JobBuilder

Full name: Hopac.TopLevel.job
val i : int
Raw view New version

More information

Link:http://fssnip.net/p1
Posted:2 years ago
Author:Vasily Kirichenko
Tags: async , hopac , joinads