Last active
April 20, 2016 14:15
-
-
Save nathanmerrill/6f8e25ecfa45d23fa2163e7930647f05 to your computer and use it in GitHub Desktop.
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
class Order(customer: Customer) { | |
option country=Countries.US | |
items: Map<Item, Integer<Positive>> | |
fn addItem(item: Item){ | |
option quantity: Integer<Positive>=1 | |
items[item] = quantity | |
} | |
fn getCount(item: Item): Int<Positive>?{ | |
items[item] | |
} | |
fn containsItem(item: Item): Bool { | |
item in items | |
} | |
fn removeItem(item: Item){ | |
items.remove(item) | |
} | |
} | |
adapt Order to Collection<Item>{ | |
add(item) -> addItem(item) | |
remove(item) -> removeItem(item) | |
} | |
adapt Order to Map<Item, Integer<Positive>>{ | |
put(item, count) -> addItem(item, quantity=count) | |
get(item) -> getCount(item) | |
has(item) -> containsItem(item) | |
remove(item) -> removeItem(item) | |
} | |
trait Positive for Int{ | |
Int.get() > 0 | |
Positive + Unsigned | |
Unsigned + Positive | |
Negative * Negative | |
Positive * Positive | |
} | |
trait Zero for Int { | |
Int.get() = 0 | |
Zero * Int | |
Int * Zero | |
} | |
trait Unsigned for Int { | |
Positive | |
Zero | |
Math.abs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment