4 people like it.
Like the snippet!
F# Future using lazy and a threading event
F# Future using lazy and a threading event.
Supports creating futures from functions or asyncs.
Eager evaluation of can be specified.
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:
|
open System
open System.Threading
module LazyFuture =
let ofAsync eager' (computation:Async<'a>) =
let result = ref Unchecked.defaultof<_>
let gate = new ManualResetEventSlim (false)
let init =
async { let! res = computation |> Async.Catch
result := res
gate.Set () }
if eager' then init |> Async.Start
lazy
if not (eager') then init |> Async.Start
gate.Wait ()
match !result with
| Choice1Of2 r -> r
| Choice2Of2 e -> raise e
let create eager' computation = ofAsync eager' (async { return computation () })
let v = LazyFuture.create false (fun () -> Thread.Sleep(3000); 10)
v.Force () |> printfn "%A"
|
namespace System
namespace System.Threading
val ofAsync : eager':bool -> computation:Async<'a> -> Lazy<'a>
Full name: Script.LazyFuture.ofAsync
val eager' : bool
val computation : Async<'a>
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<'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<_>
val result : Choice<'a,exn> ref
Multiple items
val ref : value:'T -> 'T ref
Full name: Microsoft.FSharp.Core.Operators.ref
--------------------
type 'T ref = Ref<'T>
Full name: Microsoft.FSharp.Core.ref<_>
module Unchecked
from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T
Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
val gate : ManualResetEventSlim
Multiple items
type ManualResetEventSlim =
new : unit -> ManualResetEventSlim + 2 overloads
member Dispose : unit -> unit
member IsSet : bool with get, set
member Reset : unit -> unit
member Set : unit -> unit
member SpinCount : int with get, set
member Wait : unit -> unit + 5 overloads
member WaitHandle : WaitHandle
Full name: System.Threading.ManualResetEventSlim
--------------------
ManualResetEventSlim() : unit
ManualResetEventSlim(initialState: bool) : unit
ManualResetEventSlim(initialState: bool, spinCount: int) : unit
val init : Async<unit>
val async : AsyncBuilder
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val res : Choice<'a,exn>
static member Async.Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
ManualResetEventSlim.Set() : unit
static member Async.Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
ManualResetEventSlim.Wait() : unit
ManualResetEventSlim.Wait(millisecondsTimeout: int) : bool
ManualResetEventSlim.Wait(timeout: TimeSpan) : bool
ManualResetEventSlim.Wait(cancellationToken: CancellationToken) : unit
ManualResetEventSlim.Wait(millisecondsTimeout: int, cancellationToken: CancellationToken) : bool
ManualResetEventSlim.Wait(timeout: TimeSpan, cancellationToken: CancellationToken) : bool
union case Choice.Choice1Of2: 'T1 -> Choice<'T1,'T2>
val r : 'a
union case Choice.Choice2Of2: 'T2 -> Choice<'T1,'T2>
val e : exn
val raise : exn:Exception -> 'T
Full name: Microsoft.FSharp.Core.Operators.raise
val create : eager':bool -> computation:(unit -> 'a) -> Lazy<'a>
Full name: Script.LazyFuture.create
val computation : (unit -> 'a)
val v : Lazy<int>
Full name: Script.v
module LazyFuture
from Script
Multiple items
type Thread =
inherit CriticalFinalizerObject
new : start:ThreadStart -> Thread + 3 overloads
member Abort : unit -> unit + 1 overload
member ApartmentState : ApartmentState with get, set
member CurrentCulture : CultureInfo with get, set
member CurrentUICulture : CultureInfo with get, set
member DisableComObjectEagerCleanup : unit -> unit
member ExecutionContext : ExecutionContext
member GetApartmentState : unit -> ApartmentState
member GetCompressedStack : unit -> CompressedStack
member GetHashCode : unit -> int
...
Full name: System.Threading.Thread
--------------------
Thread(start: ThreadStart) : unit
Thread(start: ParameterizedThreadStart) : unit
Thread(start: ThreadStart, maxStackSize: int) : unit
Thread(start: ParameterizedThreadStart, maxStackSize: int) : unit
Thread.Sleep(timeout: TimeSpan) : unit
Thread.Sleep(millisecondsTimeout: int) : unit
member Lazy.Force : unit -> 'T
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information