Skip to content

Instantly share code, notes, and snippets.

@nathanmerrill
Last active April 20, 2016 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanmerrill/6f8e25ecfa45d23fa2163e7930647f05 to your computer and use it in GitHub Desktop.
Save nathanmerrill/6f8e25ecfa45d23fa2163e7930647f05 to your computer and use it in GitHub Desktop.
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