Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell-wk
Created December 17, 2017 12:30
Show Gist options
  • Save dustinlacewell-wk/1b786e817b366b42ec968755b5e4f9fa to your computer and use it in GitHub Desktop.
Save dustinlacewell-wk/1b786e817b366b42ec968755b5e4f9fa to your computer and use it in GitHub Desktop.
#!/usr/bin/env fsharpi
#I "../../../.nuget/packages/hopac/0.3.21/lib/net45/"
#I "../../../.nuget/packages/hopac.extras/0.3.1/lib/net45"
#r "Hopac.Extras"
#r "Hopac.Platform"
#r "Hopac.Core"
#r "Hopac"
open System
open Hopac.Job
open Hopac
open Hopac.Infixes
open Hopac.Extensions
type Buffer<'a> =
{ emptyCh: Ch<unit>
insCh: Ch<'a>
remCh: Ch<'a>
remAckCh: Ch<unit> }
member this.Empty () = Ch.send this.emptyCh () >>=. Ch.take this.insCh >>= this.Full
member this.Full x = Ch.send this.remCh x >>=. Ch.take this.remAckCh >>= this.Empty
member this.Insert v = Ch.take this.emptyCh >>=. Ch.send this.insCh v
member this.Remove () = job { let! v = Ch.take this.remCh
do! Ch.send this.remAckCh ()
return v }
[<AutoOpen>]
module Buffer =
let create<'a> () =
{ emptyCh = Ch<unit>()
insCh = Ch<'a>()
remCh = Ch<'a>()
remAckCh = Ch<unit>() }
let insert (buf:Buffer<'a>) v = buf.Insert v
let remove (buf:Buffer<'a>) = buf.Remove ()
let printResult i x = job { printfn "%i: got %i" i x }
let length = 5
let workers = 2
// communication buffer
let (buffer:Buffer<int>) = create ()
// data to communicate
let data = [1..length]
// buffer worker
start <| buffer.Empty ()
// non-local consumers
let factory i = start <| foreverServer (remove buffer >>= printResult i)
let consumers = List.map factory [1..workers]
// local producer
run <| timeOut (TimeSpan.FromSeconds 1.0)
run <| Seq.iterJobIgnore (insert buffer) data
./2.4-async-messages.fsx
System.ArgumentException: Cannot resolve method Hopac.Job`1[a] Empty[a]() because the declaring type of the method handle FSI_0001+Buffer`1[a] is generic. Explicitly provide the declaring type to GetMethodFromHandle.
at System.Reflection.MethodBase.GetMethodFromHandle (System.RuntimeMethodHandle handle) [0x00072] in <ab2f5ad0f8c84dfe97dfd6a06a64cf44>:0
at System.Reflection.Emit.GenericTypeParameterBuilder.InternalResolve () [0x00019] in <ab2f5ad0f8c84dfe97dfd6a06a64cf44>:0
at System.Reflection.Emit.TypeBuilderInstantiation.InternalResolve () [0x0001e] in <ab2f5ad0f8c84dfe97dfd6a06a64cf44>:0
at System.Reflection.Emit.ConstructorOnTypeBuilderInst.RuntimeResolve () [0x00000] in <ab2f5ad0f8c84dfe97dfd6a06a64cf44>:0
at System.Reflection.Emit.ModuleBuilder.RuntimeResolve (System.Object obj) [0x00080] in <ab2f5ad0f8c84dfe97dfd6a06a64cf44>:0
at <StartupCode$FSI_0001>.$FSI_0001.main@ () [0x00049] in <1d7a72a5ad964b219f85f0ca6c864c6d>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <ab2f5ad0f8c84dfe97dfd6a06a64cf44>:0
Stopped due to error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment