Last active
May 17, 2024 15:09
-
-
Save eulerfx/96929afb37d826b7e6e6 to your computer and use it in GitHub Desktop.
F# event-sourcing with monoids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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