groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinko Srkoč <dinko.sr...@gmail.com>
Subject Re: closures and iterators
Date Mon, 15 Jun 2015 09:43:56 GMT
One should be careful about those nested its where explicit Closure
parameters are used:

response.data.each { it.children.each { it.children.each {
    // scope X here
    it.children.each { grower ->
        println ...
        // it.children.each { farm -> ... 'it' comes from scope X
        grower.children.each { farm -> // ok
            println ...
            // it.children.each { field -> ... 'it' is still from scope X
            farm.children.each { field -> // ok
                println ...
            }
        }
    }
}}}

Cheers,
Dinko
​

On 15 June 2015 at 07:19, David Clark <plotinussmith@gmail.com> wrote:

>  Try something like this:
>
> response.data.each {
>   it.children.each {
>     it.children.each {
>       it.children.each { grower ->        println([grower.type, grower.profile_id,
grower.name <http://it.name>, grower.created, grower.modified, "", ""].join("|"))
>         it.children.each { farm ->          println([farm.type, farm.farm_id, farm.name
<http://it.name>, farm.created, farm.modified, grower.profile_id, ""].join("|"))
>           it.children.each { field ->            println([field.type, field.field_id,
field.name <http://it.name>, field.created, field.modified, grower.profile_id, farm.farm_id].join("|"))
} } } } } }
>
> Moving the brackets is optional, it just hurts my eyes to see all of those
> dangling angle brackets each on a line by itself.
>
>
> On 06/14/2015 11:45 PM, Kurt Andrews wrote:
>
> I'm trying to figure out how to pass an extra parameter to the closure
> that's being passed to each in the following code
>
>  response.data.each {
>   it.children.each {
>     it.children.each {
>       it.children.each { // growers        println([it.type, it.profile_id, it.name,
it.created, it.modified, "", ""].join("|"))
>         it.children.each { // farms          println([it.type, it.farm_id, it.name, it.created,
it.modified, grower.profile_id, ""].join("|"))
>           it.children.each { // fields            println([it.type, it.field_id, it.name,
it.created, it.modified, grower.profile_id, farm.farm_id].join("|"))
>           }
>         }
>       }
>     }
>   }
> }
>
>  I'm trying to pass the grower profile_id down to the farms and the
> fields and pass the farms farm_id down to fields.   Is there a simple way
> to do this?
>
>  Thanks,
>
>  Kurt
>
>
>

Mime
View raw message