...
// closure list
( print "hi" ; def s = 23 ; println s )
list.each(def sum = 0;){}
list.each {
@Xyz sum = 0
sum += 1
}
// using some annotation?
list.each @Sum({}) { }
list.each { @Xyz sum = 0, elem -> sum += elem }
for (int i = 0; i < 10; i++) { ... }
// for comprehension
{ x | x in 1..3 && x % 2 == 3 }
// some iterator transformation
def fib = [
state: [a: 0, b: 1],
next: {
state.a
}, hasNext: {
(state.a, state.b) = [state.b, state.a + state.b]
true
}] as Iterator
// python generator
def fib():
a, b = 0, 1
while 1:
yield b
a, b = b, a+b
// again with some annotation
def fib = { @Xyz a = 0, @Xyz b = 1 ->
(a, b) = [b, a + b]
return a
}
| Code Block | ||||
|---|---|---|---|---|
| ||||
def fib = {
def state = [a:0, b:1 ]
state.with {
[
next: {
(a,b) = [b, a+b]
a
},
hasNext: {
b<100
}
] as Iterator
}
}
for (x in fib()) { println x } |
