Created
August 5, 2012 17:35
-
-
Save relrod/3266154 to your computer and use it in GitHub Desktop.
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
case class Person(name: String, age: Int) { | |
def <=>(person: Person) = age compare person.age | |
override def toString = name + " (" + age + ")" | |
} | |
val group = List( | |
Person("Bob", 33), | |
Person("Chris", 16), | |
Person("Ash", 23) | |
) |
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 | |
TPerson = object # I prepended a T because that is the Nimrod convention. It also gets rid of possible clashes, like in `$` :) | |
name*: string | |
age*: int | |
proc newPerson(name: string, age: int): TPerson = | |
result.name = name | |
result.age = age | |
proc `<=>`(person1: TPerson, person2: TPerson): bool = | |
person1.age <=> person2.age | |
proc `$`(person: TPerson): string = | |
return person.name & " " & person.age | |
var group: seq[TPerson] = @[ | |
newPerson("Bob", 33), | |
newPerson("Chris", 16), | |
newPerson("Ash", 23) | |
] | |
# Won't bother with sorting. :P |
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
class Person < Struct.new(:name, :age) | |
def <=>(person) # Comparison operator for sorting | |
age <=> person.age | |
end | |
def to_s | |
"#{name} (#{age})" | |
end | |
end | |
group = [ | |
Person.new("Bob", 33), | |
Person.new("Chris", 16), | |
Person.new("Ash", 23) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment