Created
April 21, 2011 04:07
-
-
Save nobeans/933698 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
// java.lang.Listにメソッドを追加する | |
List.metaClass.carcdr = { | |
[delegate.head(), delegate.tail()] | |
} | |
assert [1, 2, 3].carcdr() == [1, [2, 3]] | |
{-> | |
def (car, cdr) = [1, 2, 3].carcdr() | |
assert car == 1 | |
assert cdr == [2, 3] | |
}() | |
// ヘルパクラス(?)を追加する | |
class CarCdr { | |
def head, tail | |
CarCdr(List list) { | |
head = list.head() | |
tail = list.tail() | |
} | |
Object getAt(int index) { | |
(index == 0) ? head : tail | |
} | |
String toString() { | |
"$head : $tail" | |
} | |
} | |
{-> | |
println([1, 2, 3] as CarCdr) | |
def (car, cdr) = [1, 2, 3] as CarCdr | |
assert car == 1 | |
assert cdr == [2, 3] | |
}() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment