Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Last active May 17, 2024 15:09
Show Gist options
  • Save eulerfx/96929afb37d826b7e6e6 to your computer and use it in GitHub Desktop.
Save eulerfx/96929afb37d826b7e6e6 to your computer and use it in GitHub Desktop.
F# event-sourcing with monoids
type Monoid<'a> = {
unit : 'a
op : 'a -> 'a -> 'a
}
let endo<'a> =
{ unit = id
op = fun (f:'a -> 'a) (g:'a -> 'a) -> f << g }
type Aggregate<'State, 'Input, 'Output> = {
zero : 'State
apply : 'Output -> 'State -> 'State
exec : 'State -> 'Input -> Async<'Output>
}
/// note it is more efficient to just do Seq.fold
let currentState (aggregate:Aggregate<'State, 'Input, 'Output>) : seq<'Output> -> 'State =
(Seq.foldMap aggregate.apply endo |> flip) aggregate.zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment