Discussion:
[groovy-user] Returning from a Closure within a Function
Matthias F. Brandstetter
2015-02-18 01:30:08 UTC
Permalink
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?
--
Matthias F. Brandstetter
***@gmail.com
@maflobra <https://twitter.com/maflobra>
Nelson, Erick [HDS]
2015-02-18 01:39:41 UTC
Permalink
def val = [0,9,2,-1,8].find { it < 0 }
println val

this will print “-1”

find returns the first element in the list that is < 0 (in this example)


http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html

From: Matthias F. Brandstetter [mailto:***@gmail.com]
Sent: Tuesday, February 17, 2015 5:30 PM
To: Groovy Users
Subject: [groovy-user] Returning from a Closure within a Function

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?
--
Matthias F. Brandstetter
***@gmail.com<mailto:***@gmail.com>
@maflobra<https://twitter.com/maflobra>
Matthias F. Brandstetter
2015-02-18 09:56:43 UTC
Permalink
Thank you very much!
Post by Nelson, Erick [HDS]
def val = [0,9,2,-1,8].find { it < 0 }
println val
this will print “-1”
find returns the first element in the list that is < 0 (in this example)
http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html
*Sent:* Tuesday, February 17, 2015 5:30 PM
*To:* Groovy Users
*Subject:* [groovy-user] Returning from a Closure within a Function
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
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
@maflobra <https://twitter.com/maflobra>
--
Matthias F. Brandstetter
***@gmail.com
@maflobra <https://twitter.com/maflobra>
Dinko Srkoč
2015-02-18 10:36:44 UTC
Permalink
Post by Matthias F. Brandstetter
[...]
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.
You're right. The `return` statement from a Closure that is passed as
an argument to method `list.each` returns from the Closure, not from
the method `each`. You can see the Closure as a function that `each`
invokes. `return` thus returns from the function and `each` continues
iterating the list, passing its next element to the function.

Erick gave a link to where the method `find` is documented. It's
probably worth studying the whole of
http://docs.groovy-lang.org/docs/latest/html/groovy-jdk/ as there are
many useful methods, such as `find`, that can be found there,
enriching some of Java's standard library classes.

Cheers,
Dinko
Post by Matthias F. Brandstetter
What is the correct way to achieve what I want in Groovy?
--
Matthias F. Brandstetter
@maflobra
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...