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
type 't Iterator = End | HasMoreElements of 't * 't Iterator | |
let rec print it = | |
match it with | |
| HasMoreElements (element,next) -> printfn "Element=%A" element ; print next | |
| End -> printfn "Finished!" |
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
type Money = { | |
currency : string | |
amount : decimal | |
} |
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
public class Money : IEquatable<Money> | |
{ | |
public Money(string currency, decimal amount) | |
{ | |
this.Currency = currency; | |
this.Amount = amount; | |
} | |
public string Currency { get; private set; } | |
public decimal Amount { get; private set; } |
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
module Aggregate | |
type Aggregate<'TState, 'TCommand, 'TEvent> = { | |
zero : 'TState; | |
apply : 'TState -> 'TEvent -> 'TState; | |
exec : 'TState -> 'TCommand -> Choice<'TEvent, string list>; | |
} | |
type Id = System.Guid |
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
open Validation | |
module private Assert = | |
let validName name = notNull ["The name must not be null."] name <* notEmptyString ["The name must not be empty."] name | |
let validCount count = validator (fun c -> c > 0) ["The item count must be positive."] count | |
let inactive state = validator (fun i -> i.isActive = false) ["The item is already deactivated."] state | |
let exec state = | |
function | |
| Create name -> Assert.validName name <?> Created(name) |
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
/// Lifts a two argument function to operate over the choice type. | |
let inline lift2 f a b = f <!> a <*> b | |
/// Composes two choice types, passing the case-1 type of the right value. | |
let inline ( *>) a b = lift2 (fun _ z -> z) a b | |
/// Composes two choice types, passing the case-2 type of the left value. | |
let inline ( <*) a b = lift2 (fun z _ -> z) a b | |
/// Composes a choice type with a non choice type. |
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
/// Given a value, creates a choice 1. (Applicative functor) | |
let puree = Choice1Of2 | |
/// Given a function in a choice and a choice value x, applies the function to the value if available, | |
/// otherwise propagates the second choice. (Applicative functor) | |
let apply f x = | |
match f,x with | |
| Choice1Of2 f, Choice1Of2 x -> Choice1Of2 (f x) | |
| Choice2Of2 e, Choice1Of2 x -> Choice2Of2 e | |
| Choice1Of2 f, Choice2Of2 e -> Choice2Of2 e |
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
let validator predicate error x = | |
if predicate x then Choice1Of2 x | |
else Choice2Of2 error |
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
let exec state = | |
function | |
| Create name -> | |
match name with | |
| null | "" -> Choice2Of2 ["The name must not be null or empty."] | |
| name -> Choice1Of2 (Created(name)) |
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
module private Assert = | |
let validName name = if System.String.IsNullOrEmpty(name) then invalidArg "name" "The name must not be null." else name | |
let validCount count = if count <= 0 then invalidArg "count" "Inventory count must be positive." else count | |
let inactive item = if item.isActive = true then failwith "The item is already deactivated." | |
let exec item = | |
let apply event = | |
let newItem = apply item event | |
event | |
function |