Matthias F. Brandstetter
2015-02-18 01:30:08 UTC
Sometimes I have a function in which I iterate over a Collection, and check
its elements. When I have found the one I am looking for, I return that
value to the caller of the function. After the loop, when nothing has been
found, I would return null or something like that:
def getListVal() {
for(item in list) {
if(item.value < 0) {
return item
}
}
return null
}
Now in Groovy I try to use something like "list.each { ... }" instead.
However, if I understand correctly, when I use the same logic, i.e.
returning from within that each Closure, then the return statement would
jump out from the each Closure, but not from the whole function.
What is the correct way to achieve what I want in Groovy?
its elements. When I have found the one I am looking for, I return that
value to the caller of the function. After the loop, when nothing has been
found, I would return null or something like that:
def getListVal() {
for(item in list) {
if(item.value < 0) {
return item
}
}
return null
}
Now in Groovy I try to use something like "list.each { ... }" instead.
However, if I understand correctly, when I use the same logic, i.e.
returning from within that each Closure, then the return statement would
jump out from the each Closure, but not from the whole function.
What is the correct way to achieve what I want in Groovy?
--
Matthias F. Brandstetter
***@gmail.com
@maflobra <https://twitter.com/maflobra>
Matthias F. Brandstetter
***@gmail.com
@maflobra <https://twitter.com/maflobra>