You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Kurt Andrews <ku...@gmail.com> on 2015/06/15 06:45:44 UTC

closures and iterators

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

Re: closures and iterators

Posted by Kurt Andrews <ku...@gmail.com>.
On Mon, Jun 15, 2015 at 9:14 AM Kurt Andrews <ku...@gmail.com>
wrote:

> Thank you gentlemen, you've been a great help.  This does exactly what I
> need:
>
> <code>
> response.data.each { owner ->
>   owner.children.each { dealer ->
>     dealer.children.each { location ->
>       location.children.each { grower ->
>         println([grower.type, grower.profile_id, grower.name,
> grower.created, grower.modified, "", ""].join("|"))
>         grower.children.each { farm ->
>           println([farm.type, farm.farm_id, farm.name, farm.created,
> farm.modified, grower.profile_id, ""].join("|"))
>           farm.children.each { field ->
>             println([field.type, field.field_id, field.name,
> field.created, field.modified, grower.profile_id,
> farm.farm_id].join("|"))}}}}}}
> </code>
>
> On Mon, Jun 15, 2015 at 8:32 AM David Clark <pl...@gmail.com>
> wrote:
>
>>  Yes. My mistake, thank you for the correction.
>>
>>
>> On 06/15/2015 04:43 AM, Dinko Srkoč wrote:
>>
>>  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 <pl...@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
>>>
>>>
>>>
>>
>>

Re: closures and iterators

Posted by Kurt Andrews <ku...@gmail.com>.
Thank you gentlemen, you've been a great help.  This does exactly what I
need:

<code>
response.data.each { owner ->
  owner.children.each { dealer ->
    dealer.children.each { location ->
      location.children.each { grower ->
        println([grower.type, grower.profile_id, grower.name,
grower.created, grower.modified, "", ""].join("|"))
        grower.children.each { farm ->
          println([farm.type, farm.farm_id, farm.name, farm.created,
farm.modified, grower.profile_id, ""].join("|"))
          farm.children.each { field ->
            println([field.type, field.field_id, field.name, field.created,
field.modified, grower.profile_id, farm.farm_id].join("|"))}}}}}}
</code>

On Mon, Jun 15, 2015 at 8:32 AM David Clark <pl...@gmail.com> wrote:

>  Yes. My mistake, thank you for the correction.
>
>
> On 06/15/2015 04:43 AM, Dinko Srkoč wrote:
>
>  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 <pl...@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
>>
>>
>>
>
>

Re: closures and iterators

Posted by David Clark <pl...@gmail.com>.
Yes. My mistake, thank you for the correction.

On 06/15/2015 04:43 AM, Dinko Srkoč wrote:
>
> One should be careful about those nested |it|s 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 
> <ma...@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  <http://it.name>, it.created, it.modified,"",""].join("|"))
>>              it.children.each {// farms
>>                println([it.type, it.farm_id,it.name  <http://it.name>, it.created, it.modified,grower.profile_id,""].join("|"))
>>                it.children.each {// fields
>>                  println([it.type, it.field_id,it.name  <http://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
>
>


Re: closures and iterators

Posted by Dinko Srkoč <di...@gmail.com>.
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 <pl...@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
>
>
>

Re: closures and iterators

Posted by David Clark <pl...@gmail.com>.
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  <http://it.name>, it.created, it.modified,"",""].join("|"))
>          it.children.each {// farms
>            println([it.type, it.farm_id,it.name  <http://it.name>, it.created, it.modified,grower.profile_id,""].join("|"))
>            it.children.each {// fields
>              println([it.type, it.field_id,it.name  <http://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