Skip to content

Instantly share code, notes, and snippets.

@altbodhi
Created September 15, 2021 09:18
Show Gist options
  • Save altbodhi/46e7ac07c7645301c304eab796810e11 to your computer and use it in GitHub Desktop.
Save altbodhi/46e7ac07c7645301c304eab796810e11 to your computer and use it in GitHub Desktop.
type State =
| Ping
| Pong
type Command = | Send
type Event = | Received
let apply item =
function
| Received ->
printfn "Received %A" item
match item with
| Ping -> Pong
| Pong -> Ping
let exec state =
function
| Send -> Received
type Aggregate<'TState, 'TCommand, 'TEvent> =
{ zero: 'TState
apply: 'TState -> 'TEvent -> 'TState
exec: 'TState -> 'TCommand -> 'TEvent }
let makeHandler (aggregate: Aggregate<'TState, 'TCommand, 'TEvent>) events command =
let state =
Seq.fold aggregate.apply aggregate.zero events
aggregate.exec state command
let aggregate =
{ zero = Ping
apply = apply
exec = exec }
let events =
seq {
while true do
yield Received
Async.Sleep 1000 |> Async.RunSynchronously
}
let handler = makeHandler aggregate events
let x = handler Send
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment