Return to Snippet

Revision: 12486
at March 17, 2009 08:34 by narkisr


Initial Code
class WithIndexCategory { 
    static List withIndex(List source) { 
           def idx = 0 source.collect { [index: idx++, value: it] } 
    } 
}

use(WithIndexCategory) { l = [ 3, 1, 4, 1, 5, 9 ]

l.withIndex().each { if(it.index % 2) println it.value } }

Initial URL

                                

Initial Description
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

Initial Title
WithIndex Groovy category

Initial Tags
category, groovy

Initial Language
Groovy