Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Last active April 8, 2024 10:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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