Last active
August 29, 2015 14:04
-
-
Save JefClaes/26e159f61b06fc1712e6 to your computer and use it in GitHub Desktop.
Converted https://gist.github.com/JefClaes/496388c579dc9ce86b56 to F#
This file contains 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 Program | |
open Inventory | |
open System | |
[<EntryPoint>] | |
let main argv = | |
let item = Inventory.Register (Inventory.InventoryItemName.Make "Domain Driven Design") | |
let itemAfterCheckingIn = Inventory.CheckIn (InventoryItemCount.Make 3) item | |
let itemAfterCheckingOut = Inventory.CheckOut (InventoryItemCount.Make 1) itemAfterCheckingIn | |
let deactivatedItem = Inventory.Deactivate itemAfterCheckingOut | |
let x = Console.ReadLine() | |
0 // return an integer exit code | |
module Inventory | |
open System | |
type InventoryItemName (value: string) = | |
member this.Value = value | |
static member Make(value: string) = | |
match value with | |
| "" -> invalidArg "value" "Name shouldn't be empty." | |
| _ -> InventoryItemName value | |
type InventoryItemCount(value: int) = | |
member this.Value = value | |
static member (-) (countLeft: InventoryItemCount, countRight: InventoryItemCount) = | |
InventoryItemCount.Make(countLeft.Value-countRight.Value) | |
static member (+) (countLeft: InventoryItemCount, countRight: InventoryItemCount) = | |
InventoryItemCount.Make(countLeft.Value+countRight.Value) | |
static member Empty = | |
InventoryItemCount.Make 0 | |
static member Make(value: int) = | |
if value < 0 then | |
invalidArg "value" "Count can't be negative." | |
InventoryItemCount value | |
type InventoryItem = { Id: Guid; Activated: bool; Name: InventoryItemName; Count: InventoryItemCount; } | |
let Register name = | |
{ Id = Guid.NewGuid(); Activated = false; Name = name; Count = InventoryItemCount.Empty; } | |
let CheckIn count item = | |
{ item with Count = item.Count + count } | |
let CheckOut count item = | |
{ item with Count = item.Count - count } | |
let Deactivate item = | |
{ item with Activated = false } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't use tuples for functions arts !
let checkout count item = { ... }