8 people like it.

Async applicatives

A minimal async builder applicatives example. Also showcases fssnip support for F# preview language features.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
type AsyncBuilder with
    member async.MergeSources(left : Async<'T>, right : Async<'S>) : Async<'T * 'S> = async {
        let box f = async { let! x = f in return x :> obj }
        let! results = Async.Parallel [|box left; box right|]
        return (results.[0] :?> 'T, results.[1] :?> 'S)
    }

let test = async {
    let f x = async { let! _ = Async.Sleep 10_000 in return x }
    let! x = f false
    and! y = f 42
    and! z = f (nameof f)
    return (x, y, z)
}

#time "on"
// Real: 00:00:10.022, CPU: 00:00:00.000, GC gen0: 3, gen1: 3, gen2: 0
// val it : bool * int * string = (false, 42, "f")
Async.RunSynchronously test
type AsyncBuilder =
  private new : unit -> AsyncBuilder
  member Bind : computation:Async<'T> * binder:('T -> Async<'U>) -> Async<'U>
  member Combine : computation1:Async<unit> * computation2:Async<'T> -> Async<'T>
  member Delay : generator:(unit -> Async<'T>) -> Async<'T>
  member For : sequence:seq<'T> * body:('T -> Async<unit>) -> Async<unit>
  member Return : value:'T -> Async<'T>
  member ReturnFrom : computation:Async<'T> -> Async<'T>
  member TryFinally : computation:Async<'T> * compensation:(unit -> unit) -> Async<'T>
  member TryWith : computation:Async<'T> * catchHandler:(exn -> Async<'T>) -> Async<'T>
  member Using : resource:'T * binder:('T -> Async<'U>) -> Async<'U> (requires 'T :> IDisposable)
  ...
val async : AsyncBuilder
val left : Async<'T>
Multiple items
type Async =
  static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
  static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
  static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
  static member AwaitTask : task:Task -> Async<unit>
  static member AwaitTask : task:Task<'T> -> Async<'T>
  static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
  static member CancelDefaultToken : unit -> unit
  static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
  static member Choice : computations:seq<Async<'T option>> -> Async<'T option>
  static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
  ...

--------------------
type Async<'T> =
val right : Async<'S>
val box : (Async<'a> -> Async<obj>)
val f : Async<'a>
val x : 'a
type obj = System.Object
val results : obj []
static member Async.Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member Async.Parallel : computations:seq<Async<'T>> * ?maxDegreeOfParallelism:int -> Async<'T []>
val test : Async<bool * int * string>
val f : ('a -> Async<'a>)
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
val x : bool
val y : int
val z : string
val nameof : 'T -> string
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:System.Threading.CancellationToken -> 'T

More information

Link:http://fssnip.net/7XZ
Posted:1 month ago
Author:Eirik Tsarpalis
Tags: #applicatives