2 people like it.

Experimenting with Hopac

We sometimes come across business processes where it's important to respond within a particular time window. Here's an experiment modelling that with Hopac

 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: 
open System
open Hopac

type Expiring =
    { Complete : unit -> unit }

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Expiring =
    let create name minTime windowTime =
        let completeCh = Ch.Now.create()
        let expire =
            job {
                let! stage1 =
                    Alt.choose [
                        Ch.take completeCh |> Alt.map (fun () -> printfn "%s: early" name; Choice1Of2 ())
                        Timer.Global.timeOut minTime |> Alt.map (fun () -> Choice2Of2 ())
                    ]
                match stage1 with
                | Choice1Of2 () -> return ()
                | Choice2Of2 () ->
                    return!
                        Alt.choose [
                            Ch.take completeCh |> Alt.map (fun () -> printfn "%s: in time" name)
                            Timer.Global.timeOut windowTime |> Alt.map (fun () -> printfn "%s: late" name)
                        ]
            }
        run <| Job.start expire
        { Complete = fun () -> run <| Ch.give completeCh () }

let e1 = Expiring.create "e1" (TimeSpan.FromSeconds 1.) (TimeSpan.FromSeconds 1.)
let e2 = Expiring.create "e2" (TimeSpan.FromSeconds 1.) (TimeSpan.FromSeconds 1.)
let e3 = Expiring.create "e3" (TimeSpan.FromSeconds 1.) (TimeSpan.FromSeconds 1.)

printfn "Completing e1 early!"
e1.Complete ()

Async.RunSynchronously <| Async.Sleep 1500
printfn "Completing e2 on time!"
e2.Complete ()

printfn "Waiting for e3 to time out..."

Console.ReadLine() |> ignore
namespace System
namespace Hopac
type Expiring =
  {Complete: unit -> unit;}

Full name: Script.Expiring
Expiring.Complete: unit -> unit
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type CompilationRepresentationAttribute =
  inherit Attribute
  new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
  member Flags : CompilationRepresentationFlags

Full name: Microsoft.FSharp.Core.CompilationRepresentationAttribute

--------------------
new : flags:CompilationRepresentationFlags -> CompilationRepresentationAttribute
type CompilationRepresentationFlags =
  | None = 0
  | Static = 1
  | Instance = 2
  | ModuleSuffix = 4
  | UseNullAsTrueValue = 8
  | Event = 16

Full name: Microsoft.FSharp.Core.CompilationRepresentationFlags
CompilationRepresentationFlags.ModuleSuffix: CompilationRepresentationFlags = 4
val create : name:string -> minTime:TimeSpan -> windowTime:TimeSpan -> Expiring

Full name: Script.ExpiringModule.create
val name : string
val minTime : TimeSpan
val windowTime : TimeSpan
val completeCh : Ch<unit>
Multiple items
type Ch<'T> =
  inherit Alt<'T>
  new : unit -> Ch<'T>

Full name: Hopac.Ch<_>

--------------------
Ch() : unit
module Now

from Hopac.Ch
val create : unit -> Ch<'x>

Full name: Hopac.Ch.Now.create
val expire : Job<unit>
val job : JobBuilder

Full name: Hopac.TopLevel.job
val stage1 : Choice<unit,unit>
type Alt<'T> =
  inherit Job<'T>

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

Full name: Hopac.Alt.choose
val take : Ch<'x> -> Alt<'x>

Full name: Hopac.Ch.take
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
union case Choice.Choice1Of2: 'T1 -> Choice<'T1,'T2>
module Timer

from Hopac
module Global

from Hopac.Timer
val timeOut : TimeSpan -> Alt<unit>

Full name: Hopac.Timer.Global.timeOut
union case Choice.Choice2Of2: 'T2 -> Choice<'T1,'T2>
val run : Job<'x> -> 'x

Full name: Hopac.TopLevel.run
type Job<'T> =

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

Full name: Hopac.Job.start
val give : Ch<'x> -> 'x -> Alt<unit>

Full name: Hopac.Ch.give
val e1 : Expiring

Full name: Script.e1
Multiple items
module Expiring

from Script

--------------------
type Expiring =
  {Complete: unit -> unit;}

Full name: Script.Expiring
Multiple items
type TimeSpan =
  struct
    new : ticks:int64 -> TimeSpan + 3 overloads
    member Add : ts:TimeSpan -> TimeSpan
    member CompareTo : value:obj -> int + 1 overload
    member Days : int
    member Duration : unit -> TimeSpan
    member Equals : value:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member Hours : int
    member Milliseconds : int
    member Minutes : int
    ...
  end

Full name: System.TimeSpan

--------------------
TimeSpan()
TimeSpan(ticks: int64) : unit
TimeSpan(hours: int, minutes: int, seconds: int) : unit
TimeSpan(days: int, hours: int, minutes: int, seconds: int) : unit
TimeSpan(days: int, hours: int, minutes: int, seconds: int, milliseconds: int) : unit
TimeSpan.FromSeconds(value: float) : TimeSpan
val e2 : Expiring

Full name: Script.e2
val e3 : Expiring

Full name: Script.e3
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<_>
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:Threading.CancellationToken -> 'T
static member Async.Sleep : millisecondsDueTime:int -> Async<unit>
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.ReadLine() : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view Test code New version

More information

Link:http://fssnip.net/pB
Posted:9 years ago
Author:mavnn
Tags: hopac