Created
April 20, 2016 08:10
-
-
Save Akii/967210879fb30f300b8286e507c85936 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{-# LANGUAGE TypeFamilies #-} | |
module Ddd.Projection where | |
import Prelude hiding (init) | |
import Data.List (foldl') | |
class Projection s where | |
data Event s :: * | |
init :: s | |
apply :: s -> Event s -> s | |
load :: Projection s => [Event s] -> s | |
load = foldl' apply init | |
-- example | |
newtype State = State Int deriving Show | |
instance Projection State where | |
-- these must come from outside e. g. different module | |
data Event State = Increased | Decreased | |
init = State 0 | |
apply (State n) e = State $ case e of | |
Increased -> n + 1 | |
Decreased -> n - 1 | |
--load [Increased, Increased, Decreased, Increased, Increased] -- "State 3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment