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: 
20: 
type AsyncBuilder with
    member async.MergeSources(left : Async<'T>, right : Async<'S>) : Async<'T * 'S> = async {
        let! leftTask = Async.StartChild left in
        let! rightResult = right in
        let! leftResult = leftTask in
        return (leftResult, rightResult)
    }

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"
Async.RunSynchronously test
// Real: 00:00:05.022, CPU: 00:00:00.000, GC gen0: 3, gen1: 3, gen2: 0
// val it : bool * int * string = (false, 42, "f")
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 leftTask : Async<'T>
static member Async.StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
val rightResult : 'S
val leftResult : 'T
val test : Async<bool * int * string>
val f : ('a -> Async<'a>)
val x : '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
Next Version Raw view Test code New version

More information

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