Posted By

narkisr on 03/17/09


Tagged

category groovy


Versions (?)

WithIndex Groovy category


 / Published in: Groovy
 

An example of utilizing a category in Groovy in order to create withIndexfunctionality on lists each methods (traversing the list and having the index on each step), @ http://markmail.org/message/pa2irg4mvydpxi7c

  1. class WithIndexCategory {
  2. static List withIndex(List source) {
  3. def idx = 0 source.collect { [index: idx++, value: it] }
  4. }
  5. }
  6.  
  7. use(WithIndexCategory) { l = [ 3, 1, 4, 1, 5, 9 ]
  8.  
  9. l.withIndex().each { if(it.index % 2) println it.value } }

Report this snippet  

You need to login to post a comment.