Skip to content

Instantly share code, notes, and snippets.

@Akii
Created April 20, 2016 08:10
Show Gist options
  • Save Akii/967210879fb30f300b8286e507c85936 to your computer and use it in GitHub Desktop.
Save Akii/967210879fb30f300b8286e507c85936 to your computer and use it in GitHub Desktop.
{-# 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