CQRS and functional programming
The general response was:
What’s missing from Greg’s example?
And my reply was that nothing was missing. As people seemed to be flabbergasted by this code, I decided to convert it into a blog post.
This is the code:
A little background for the people who don’t know Haskell:
datastatements define all the required commands, events and state; a pipe symbol for the data type means A
handle :: [Event] -> Command -> [Event]implies that it is a function named
handlethat takes as parameters an array of events and a command, and it returns an array of Events.
handlefirst creates an
apply eventsto it, then it passes it into a function named
handle', which uses pattern matching to figure out which
item(i.e. the state) is either
applyevents starts out with
Nothingas the state, and then calls
apply'for every event in the events, resulting in the modified state upon applying every event.
I didn’t consider this such a big thing, except for the terseness and the very low signal to noise ratio in the code, but as people seemed to like it I decided to put this in a blog post for your enjoyment.