F# writer monad for evented computations
dotnet add package NBB.Core.Evented.FSharp
An evented value is a product of some value as the payload and a list of events
type Evented<'a, 'e> = Evented of payload:'a * events:'e list
let (<!>) = Evented.map
let (<*>) = Evented.apply
let (>>=) evented func = Evented.bind func evented
let (>=>) = Evented.composeK
Inside evented ces you can accumulate events in an imperative fashion with the addEvent function
let create (id,amount) =
evented{
let payment = {Id=id; Amount=amount; Cancelled=false}
do! addEvent <| Added(id,amount)
return payment
}
In the sample below we model some evented domain using the evented monad
module Sample =
type Payment = {
Id: int
Amount: decimal
Cancelled: bool
}
type DomainEvent =
| Added of Id:int * Amount:decimal
| Cancelled of Id:int
let create (id,amount) =
evented{
let payment = {Id=id; Amount=amount; Cancelled=false}
do! addEvent <| Added(id,amount)
return payment
}
let cancel payment =
evented{
let payment' = {payment with Cancelled=true}
do! addEvent <| Cancelled payment.Id
return payment'
}
let createCancelled = create >=> cancel