
is a functional language for the working hacker
enum Shape {
Square(Int)
Rectangle(Int, Int)
}
interface Area<T> {
let area : (T) -> Int
}
implementation Area<Shape> {
fn area(s) {
match s {
case Square(x): x * x
case Rectangle(x, y): x * y
}
}
}
Square(10).area() // => 100 : Int
Rectangle(4, 5).area() // => 20 : Int
class House {
let rooms : List<Shape>
}
implementation Area<House> {
fn area(h) {
h.rooms.map(area).reduce((+), 0)
}
}
House { rooms: [Square(10), Rectangle(4, 5)] }.area() // => 120 : Int