2 people like it.

inline rec functions

"let rec inline" doesn't always behave as expected

 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: 
53: 
54: 
55: 
56: 
57: 
58: 
open System
open EventStore.ClientAPI
open Chiron

let store = 
    (ConnectionSettings.Create()
        .UseConsoleLogger()
        .Build(),
     System.Net.IPEndPoint(System.Net.IPAddress.Parse "192.168.33.10", 1113))
    |> EventStoreConnection.Create

store.ConnectAsync().Wait()

let inline encode event =
    Json.serialize event
    |> Json.format 
    |> System.Text.Encoding.UTF8.GetBytes

let inline decode (bytes : byte []) =
    bytes
    |> System.Text.Encoding.UTF8.GetString
    |> fun s ->
        printfn "%s" s
        s
    |> Json.parse
    |> Json.deserialize

let rec inline before currentState eventNumber fold streamName =
    async {
        let! slice =
            store.ReadStreamEventsForwardAsync(streamName, eventNumber, 10, true)
            |> Async.AwaitTask
        let nextState =
            slice.Events
            |> Seq.map (fun e -> e.Event.Data |> decode)
            |> Seq.fold fold currentState
        if slice.IsEndOfStream then
            return (nextState, slice.NextEventNumber)
        else
            return! refresh' nextState slice.NextEventNumber fold streamName
    }

let inline after currentState eventNumber fold streamName =
    let rec inner currentState eventNumber fold streamName =
        async {
            let! slice =
                store.ReadStreamEventsForwardAsync(streamName, eventNumber, 10, true)
                |> Async.AwaitTask
            let nextState =
                slice.Events
                |> Seq.map (fun e -> e.Event.Data |> decode)
                |> Seq.fold fold currentState
            if slice.IsEndOfStream then
                return (nextState, slice.NextEventNumber)
            else
                return! inner nextState slice.NextEventNumber fold streamName
        }
    inner currentState eventNumber fold streamName
namespace System
namespace EventStore
namespace EventStore.ClientAPI
module Chiron
val store : IEventStoreConnection

Full name: Script.store
type ConnectionSettings =
  val Log : ILogger
  val VerboseLogging : bool
  val MaxQueueSize : int
  val MaxConcurrentItems : int
  val MaxRetries : int
  val MaxReconnections : int
  val RequireMaster : bool
  val ReconnectionDelay : TimeSpan
  val OperationTimeout : TimeSpan
  val OperationTimeoutCheckPeriod : TimeSpan
  ...

Full name: EventStore.ClientAPI.ConnectionSettings
ConnectionSettings.Create() : ConnectionSettingsBuilder
namespace System.Net
Multiple items
type IPEndPoint =
  inherit EndPoint
  new : address:int64 * port:int -> IPEndPoint + 1 overload
  member Address : IPAddress with get, set
  member AddressFamily : AddressFamily
  member Create : socketAddress:SocketAddress -> EndPoint
  member Equals : comparand:obj -> bool
  member GetHashCode : unit -> int
  member Port : int with get, set
  member Serialize : unit -> SocketAddress
  member ToString : unit -> string
  static val MinPort : int
  ...

Full name: System.Net.IPEndPoint

--------------------
Net.IPEndPoint(address: int64, port: int) : unit
Net.IPEndPoint(address: Net.IPAddress, port: int) : unit
Multiple items
type IPAddress =
  new : newAddress:int64 -> IPAddress + 2 overloads
  member Address : int64 with get, set
  member AddressFamily : AddressFamily
  member Equals : comparand:obj -> bool
  member GetAddressBytes : unit -> byte[]
  member GetHashCode : unit -> int
  member IsIPv6LinkLocal : bool
  member IsIPv6Multicast : bool
  member IsIPv6SiteLocal : bool
  member IsIPv6Teredo : bool
  ...

Full name: System.Net.IPAddress

--------------------
Net.IPAddress(newAddress: int64) : unit
Net.IPAddress(address: byte []) : unit
Net.IPAddress(address: byte [], scopeid: int64) : unit
Net.IPAddress.Parse(ipString: string) : Net.IPAddress
type EventStoreConnection =
  static member Create : uri:Uri * ?connectionName:string -> IEventStoreConnection + 6 overloads

Full name: EventStore.ClientAPI.EventStoreConnection
EventStoreConnection.Create(tcpEndPoint: Net.IPEndPoint, ?connectionName: string) : IEventStoreConnection
EventStoreConnection.Create(connectionSettings: ConnectionSettings, ?connectionName: string) : IEventStoreConnection
EventStoreConnection.Create(connectionString: string, ?connectionName: string) : IEventStoreConnection
EventStoreConnection.Create(uri: Uri, ?connectionName: string) : IEventStoreConnection
EventStoreConnection.Create(connectionSettings: ConnectionSettings, clusterSettings: ClusterSettings, ?connectionName: string) : IEventStoreConnection
EventStoreConnection.Create(connectionSettings: ConnectionSettings, tcpEndPoint: Net.IPEndPoint, ?connectionName: string) : IEventStoreConnection
EventStoreConnection.Create(connectionSettings: ConnectionSettings, uri: Uri, ?connectionName: string) : IEventStoreConnection
IEventStoreConnection.ConnectAsync() : Threading.Tasks.Task
val encode : event:'a -> 'b

Full name: Script.encode
val event : 'a
Multiple items
module Json

from Chiron.Mapping

--------------------
module Json

from Chiron.Formatting

--------------------
module Json

from Chiron.Parsing

--------------------
module Json

from Chiron.Optics

--------------------
module Json

from Chiron.Functional

--------------------

--------------------
type Json<'a> = Json -> JsonResult<'a> * Json

Full name: Chiron.Functional.Json<_>
namespace System.Text
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
property Text.Encoding.UTF8: Text.Encoding
Text.Encoding.GetBytes(s: string) : byte []
Text.Encoding.GetBytes(chars: char []) : byte []
Text.Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
Text.Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
Text.Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Text.Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
val decode : bytes:byte [] -> 'a

Full name: Script.decode
val bytes : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = Byte

Full name: Microsoft.FSharp.Core.byte
Text.Encoding.GetString(bytes: byte []) : string
Text.Encoding.GetString(bytes: byte [], index: int, count: int) : string
val s : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val before : currentState:'a -> eventNumber:int -> fold:('a -> 'b -> 'a) -> streamName:string -> Async<'a * int>

Full name: Script.before
val currentState : 'a
val eventNumber : int
val fold : ('a -> 'b -> 'a)
val streamName : string
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val slice : StreamEventsSlice
IEventStoreConnection.ReadStreamEventsForwardAsync(stream: string, start: int, count: int, resolveLinkTos: bool, ?userCredentials: SystemData.UserCredentials) : Threading.Tasks.Task<StreamEventsSlice>
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.AwaitTask : task:Threading.Tasks.Task<'T> -> Async<'T>
val nextState : 'a
field StreamEventsSlice.Events
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val e : ResolvedEvent
field ResolvedEvent.Event
field RecordedEvent.Data
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
field StreamEventsSlice.IsEndOfStream
field StreamEventsSlice.NextEventNumber
val after : currentState:'a -> eventNumber:int -> fold:('a -> 'b -> 'a) -> streamName:string -> Async<'a * int>

Full name: Script.after
val inner : ('c -> int -> ('c -> 'd -> 'c) -> string -> Async<'c * int>)
val currentState : 'c
val fold : ('c -> 'd -> 'c)
val nextState : 'c
Raw view Test code New version

More information

Link:http://fssnip.net/qe
Posted:9 years ago
Author:mavnn
Tags: rec , inline , eventstore