WithIndex Groovy category


/ Published in: Groovy
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.