7 people like it.

Easy task creation

Lightweight syntax for creating tasks using computation expressions

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
open System.Threading
open System.Threading.Tasks

[<AbstractClass>]
type AsyncBuilderAbstract() =
    // AsyncBuilder is marked sealed, so we need this wrapper
    member __.Zero() = async.Zero()
    member __.Return t = async.Return t
    member __.ReturnFrom t = async.ReturnFrom t
    member __.Bind(f,g) = async.Bind(f,g)
    member __.Combine(f,g) = async.Combine(f,g)
    member __.Delay f = async.Delay f
    member __.While(c,b) = async.While(c,b)
    member __.For(xs,b) = async.For(xs,b)
    member __.TryWith(b,e) = async.TryWith(b,e)

type TaskBuilder(?ct : CancellationToken) =
    inherit AsyncBuilderAbstract()
    member __.Run f : Task<'T> = Async.StartAsTask(f, ?cancellationToken = ct)

type UntypedTaskBuilder(?ct : CancellationToken) =
    inherit AsyncBuilderAbstract()
    member __.Run f : Task = Async.StartAsTask(f, ?cancellationToken = ct) :> Task

let task = new TaskBuilder()
let taskC ct = new TaskBuilder(ct)
let taskU = new UntypedTaskBuilder()

// Examples

// Before:
let ct = new CancellationToken()

async {
    for i in 1 .. 100 do 
        printfn "%d" i
        do! Async.Sleep 10
} |> fun a -> Async.StartAsTask(a, cancellationToken = ct)

// After:
task {
    for i in 1 .. 100 do
        printfn "%d" i
        do! Async.Sleep 10
}

// with cancellation:
taskC ct {
    for i in 1 .. 100 do
        printfn "%d" i
        do! Async.Sleep 10
}
namespace System
namespace System.Threading
namespace System.Threading.Tasks
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

Full name: Microsoft.FSharp.Core.AbstractClassAttribute

--------------------
new : unit -> AbstractClassAttribute
Multiple items
type AsyncBuilderAbstract =
  new : unit -> AsyncBuilderAbstract
  member Bind : f:Async<'e> * g:('e -> Async<'f>) -> Async<'f>
  member Combine : f:Async<unit> * g:Async<'d> -> Async<'d>
  member Delay : f:(unit -> Async<'c>) -> Async<'c>
  member For : xs:seq<'b> * b:('b -> Async<unit>) -> Async<unit>
  member Return : t:'h -> Async<'h>
  member ReturnFrom : t:Async<'g> -> Async<'g>
  member TryWith : b:Async<'a> * e:(exn -> Async<'a>) -> Async<'a>
  member While : c:(unit -> bool) * b:Async<unit> -> Async<unit>
  member Zero : unit -> Async<unit>

Full name: Script.AsyncBuilderAbstract

--------------------
new : unit -> AsyncBuilderAbstract
member AsyncBuilderAbstract.Zero : unit -> Async<unit>

Full name: Script.AsyncBuilderAbstract.Zero
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
member AsyncBuilder.Zero : unit -> Async<unit>
val __ : AsyncBuilderAbstract
member AsyncBuilderAbstract.Return : t:'h -> Async<'h>

Full name: Script.AsyncBuilderAbstract.Return
val t : 'h
member AsyncBuilder.Return : value:'T -> Async<'T>
member AsyncBuilderAbstract.ReturnFrom : t:Async<'g> -> Async<'g>

Full name: Script.AsyncBuilderAbstract.ReturnFrom
val t : Async<'g>
member AsyncBuilder.ReturnFrom : computation:Async<'T> -> Async<'T>
member AsyncBuilderAbstract.Bind : f:Async<'e> * g:('e -> Async<'f>) -> Async<'f>

Full name: Script.AsyncBuilderAbstract.Bind
val f : Async<'e>
val g : ('e -> Async<'f>)
member AsyncBuilder.Bind : computation:Async<'T> * binder:('T -> Async<'U>) -> Async<'U>
member AsyncBuilderAbstract.Combine : f:Async<unit> * g:Async<'d> -> Async<'d>

Full name: Script.AsyncBuilderAbstract.Combine
val f : Async<unit>
val g : Async<'d>
member AsyncBuilder.Combine : computation1:Async<unit> * computation2:Async<'T> -> Async<'T>
member AsyncBuilderAbstract.Delay : f:(unit -> Async<'c>) -> Async<'c>

Full name: Script.AsyncBuilderAbstract.Delay
val f : (unit -> Async<'c>)
member AsyncBuilder.Delay : generator:(unit -> Async<'T>) -> Async<'T>
member AsyncBuilderAbstract.While : c:(unit -> bool) * b:Async<unit> -> Async<unit>

Full name: Script.AsyncBuilderAbstract.While
val c : (unit -> bool)
val b : Async<unit>
member AsyncBuilder.While : guard:(unit -> bool) * computation:Async<unit> -> Async<unit>
member AsyncBuilderAbstract.For : xs:seq<'b> * b:('b -> Async<unit>) -> Async<unit>

Full name: Script.AsyncBuilderAbstract.For
val xs : seq<'b>
val b : ('b -> Async<unit>)
member AsyncBuilder.For : sequence:seq<'T> * body:('T -> Async<unit>) -> Async<unit>
member AsyncBuilderAbstract.TryWith : b:Async<'a> * e:(exn -> Async<'a>) -> Async<'a>

Full name: Script.AsyncBuilderAbstract.TryWith
val b : Async<'a>
val e : (exn -> Async<'a>)
member AsyncBuilder.TryWith : computation:Async<'T> * catchHandler:(exn -> Async<'T>) -> Async<'T>
Multiple items
type TaskBuilder =
  inherit AsyncBuilderAbstract
  new : ?ct:CancellationToken -> TaskBuilder
  member Run : f:Async<'T> -> Task<'T>

Full name: Script.TaskBuilder

--------------------
new : ?ct:CancellationToken -> TaskBuilder
val ct : CancellationToken option
Multiple items
type CancellationToken =
  struct
    new : canceled:bool -> CancellationToken
    member CanBeCanceled : bool
    member Equals : other:CancellationToken -> bool + 1 overload
    member GetHashCode : unit -> int
    member IsCancellationRequested : bool
    member Register : callback:Action -> CancellationTokenRegistration + 3 overloads
    member ThrowIfCancellationRequested : unit -> unit
    member WaitHandle : WaitHandle
    static member None : CancellationToken
  end

Full name: System.Threading.CancellationToken

--------------------
CancellationToken()
CancellationToken(canceled: bool) : unit
member TaskBuilder.Run : f:Async<'T> -> Task<'T>

Full name: Script.TaskBuilder.Run
val f : Async<'T>
Multiple items
type Task =
  new : action:Action -> Task + 7 overloads
  member AsyncState : obj
  member ContinueWith : continuationAction:Action<Task> -> Task + 9 overloads
  member CreationOptions : TaskCreationOptions
  member Dispose : unit -> unit
  member Exception : AggregateException
  member Id : int
  member IsCanceled : bool
  member IsCompleted : bool
  member IsFaulted : bool
  ...

Full name: System.Threading.Tasks.Task

--------------------
type Task<'TResult> =
  inherit Task
  new : function:Func<'TResult> -> Task<'TResult> + 7 overloads
  member ContinueWith : continuationAction:Action<Task<'TResult>> -> Task + 9 overloads
  member Result : 'TResult with get, set
  static member Factory : TaskFactory<'TResult>

Full name: System.Threading.Tasks.Task<_>

--------------------
Task(action: System.Action) : unit
Task(action: System.Action, cancellationToken: CancellationToken) : unit
Task(action: System.Action, creationOptions: TaskCreationOptions) : unit
Task(action: System.Action<obj>, state: obj) : unit
Task(action: System.Action, cancellationToken: CancellationToken, creationOptions: TaskCreationOptions) : unit
Task(action: System.Action<obj>, state: obj, cancellationToken: CancellationToken) : unit
Task(action: System.Action<obj>, state: obj, creationOptions: TaskCreationOptions) : unit
Task(action: System.Action<obj>, state: obj, cancellationToken: CancellationToken, creationOptions: TaskCreationOptions) : unit

--------------------
Task(function: System.Func<'TResult>) : unit
Task(function: System.Func<'TResult>, cancellationToken: CancellationToken) : unit
Task(function: System.Func<'TResult>, creationOptions: TaskCreationOptions) : unit
Task(function: System.Func<obj,'TResult>, state: obj) : unit
Task(function: System.Func<'TResult>, cancellationToken: CancellationToken, creationOptions: TaskCreationOptions) : unit
Task(function: System.Func<obj,'TResult>, state: obj, cancellationToken: CancellationToken) : unit
Task(function: System.Func<obj,'TResult>, state: obj, creationOptions: TaskCreationOptions) : unit
Task(function: System.Func<obj,'TResult>, state: obj, cancellationToken: CancellationToken, creationOptions: TaskCreationOptions) : unit
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 FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
Multiple items
type UntypedTaskBuilder =
  inherit AsyncBuilderAbstract
  new : ?ct:CancellationToken -> UntypedTaskBuilder
  member Run : f:Async<'a> -> Task

Full name: Script.UntypedTaskBuilder

--------------------
new : ?ct:CancellationToken -> UntypedTaskBuilder
member UntypedTaskBuilder.Run : f:Async<'a> -> Task

Full name: Script.UntypedTaskBuilder.Run
val f : Async<'a>
val task : TaskBuilder

Full name: Script.task
val taskC : ct:CancellationToken -> TaskBuilder

Full name: Script.taskC
val ct : CancellationToken
val taskU : UntypedTaskBuilder

Full name: Script.taskU
val ct : CancellationToken

Full name: Script.ct
val i : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
val a : Async<unit>
Raw view New version

More information

Link:http://fssnip.net/7TE
Posted:14 days ago
Author:Eirik Tsarpalis
Tags: computation expressions , tasks