Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Observation a
- = ObservedValue a
- | ObservedError Exception
- | DoneObserving
- observable: (Observation a -> ()) -> ()
- observer : Observation a -> ()
- enumerable: () -> (() -> Observation a)
- enumerator: () -> Observation a
- ---------------------------------------------------
- domain Bookkeeping
- type alias AccountingSystemId = String
- type alias EntityName = String
- type alias AccountingSystemStatus = Created | Preparing | Ready | Forgotten
- type alias AccountingSystemUse = Empty | Domestic | SelfEmployed | SmallBusiness | Business | Enterprise
- aggregate a: AccountingSystem
- identifier id: AccountingSystemId
- field name: EntityName
- field isActive: Boolean
- field status: AccountingSystemStatus
- new event Created { name: EntityName } = { name = name, isActive = false, status = Created }
- event Preparing { use: AccountingSystemUse } = { a | status = Preparing }
- event SetReady {} = { a | status = Ready }
- event Forgotten {} = { a | status = Forgotten }
- // event CreationRejected { name: EntityName } = a // this is a transient event or message, goes to the hub but do not gets registered by the aggregate because it does not change state or ever will ¿really?
- transient event CreationRejected { name: EntityName }
- transient event PreparingRejected { use: AccountingSystemUse }
- transient event FinishPreparationRejected { }
- transient event SetReadyRejected { }
- transient event NoOp {}
- event Activated {} = { a | isActive = True }
- event Deactivated {} = { a | isActive = False }
- new command Create { name: EntityName } =
- case isValidEntityName name of
- True -> [ Created { name = name } ]
- False -> [ CreationRejected { name = name } ]
- command PrepareForUse { use: AccountingSystemUse } =
- case a.status of
- Created -> [ Preparing { use = use } ]
- _ -> [ PreparingRejected { use = use } ]
- command FinishPreparation {} =
- case a.status of
- Preparing -> [ SetReady {} ]
- _ -> [ SetReadyRejected {} ]
- command Forget {} =
- case a.status of
- Created -> [ Forgotten { } ]
- Ready -> [ Forgotten { } ]
- _ -> [ ForgettingRejected { } ] // Or maybe stop preparation
- command Activate {} = [ Activated {} ]
- command Deactivate {} = [ Deactivated {} ]
- process p: PrepareAccountingSystemForDomesticUse
- identifier id: String
- new event e: AccountingSystem.Created
- SQL-projection p: AccountingSystemList
- function isValidEntityName name: EntityName = Regex.isMatch name @"[\w]{5, 20}"
- ---------------------------------------------------
- module SortedList (SortedList, new, add, ...) where
- type Comparison
- = Equals
- | LeftLessThanRight
- | LeftGreaterThanRight
- type alias AvlNode a
- { value : a
- , count : Int
- , height: Int
- , left : Maybe (Tree a)
- , right : Maybe (Tree a)
- }
- type alias SortedList a =
- { root: Maybe (AvlNode a)
- , comparer: a -> a -> Comparison
- }
- new: (a -> a -> Comparison) -> SortedList a
- new comparer =
- { root = Nothing
- , comparer = comparer
- }
- count: SortedList a -> Comparison
- count {_ | root: Maybe (AvlNode a)} =
- case root of
- Nothing -> 0
- Just node -> node.count
- add: SortedList a -> a -> SortedList a
- add list value =
- let
- newRoot = Just (addValue list.comparer list.root value)
- in
- { list | root = newRoot }
- ---------------------------------
- addValue: (a -> a -> Comparison) -> Maybe(AvlNode a) -> a -> AvlNode a
- addValue comparer tree value =
- nodeHeight: Maybe (AvlNode a) -> Int
- nodeHeight node =
- case node of
- Nothing ->
- 0
- Just n ->
- n.height
- nodeCount: Maybe (AvlNode a) -> Int
- nodeCount node =
- case node of
- Nothing ->
- 0
- Just n ->
- n.count
- insertAsNode: (a -> a -> Comparison) -> Maybe (AvlTree a) -> a -> Maybe (AvlTree a)
- insertAsNode comparer root value =
- case root of
- Nothing ->
- { value = value
- , count = 1
- , height = 1
- , left = Nothing
- , right = Nothing
- }
- Just node ->
- case comparer node.value value of
- Equals | LeftLessThanRight ->
- let
- newRoot = updateCountHeight
- { node
- | left = insertAsNode comparer node.left value
- , right = node.right
- }
- in
- balanceNode (Just newRoot)
- LeftGreaterThanRight ->
- let
- newRoot = updateCountHeight
- { node
- | left = node.left
- , right = insertAsNode comparer node.right value
- }
- in
- balanceNode (Just newRoot)
- deleteAsNode: (a -> a -> Comparison) -> Maybe (AvlTree a) -> a -> Maybe (AvlTree a)
- deleteAsNode comparer root value =
- case root of
- Nothing ->
- Nothing
- Just node ->
- case comparer node.value value of
- LeftLessThanRight ->
- let
- newRoot = updateCountHeight
- { node
- | right = deleteAsNode comparer node.right value
- }
- in
- balanceNode (Just newRoot)
- LeftGreaterThanRight ->
- let
- newRoot = updateCountHeight
- { node
- | left = deleteAsNode comparer node.left value
- }
- in
- balanceNode (Just newRoot)
- Equals ->
- balanceNode: Maybe (AvlTree a) -> Maybe (AvlTree a)
- balanceNode root =
- case root of
- Nothing ->
- Nothing
- Just node ->
- let
- lh = nodeHeight node.left
- rh = nodeHeight node.right
- diff = Math.abs (lh - rh)
- in
- if diff <= 1
- then
- node
- else
- case node of
- { na
- | left:
- { nb
- | left: t1
- , right:
- { nc
- | left: t2
- , right: t3
- }
- }
- , right = t4
- } ->
- updateCountHeight
- { nc
- | left = updateCountHeight
- { nb
- | left = t1
- , right = t2
- }
- , right = updateCountHeight
- { na
- | left = t3
- , right = t4
- }
- }
- { na
- , left = t1
- | right:
- { nb
- | left:
- { nc
- | left: t2
- , right: t3
- }
- , right: t4
- }
- } ->
- updateCountHeight
- { nc
- | left = updateCountHeight
- { na
- | left = t1
- , right = t2
- }
- , right = updateCountHeight
- { nb
- | left = t3
- , right = t4
- }
- }
- _ ->
- node
- updateCountHeight: Maybe (AvlTree a) -> Maybe (AvlTree a)
- updateCountHeight root =
- case root of
- Nothing ->
- Nothing
- Just node ->
- Just
- { node
- | count = (nodeCount node.left) + (nodeCount node.right) + 1
- , height = (Math.max (nodeHeight node.left) (nodeHeight node.right)) + 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement