Roman
Roman

Reputation: 8231

Divide a collection into bulks and invoke a closure on each bulk in Groovy

I wonder there might be a built-in feature in Groovy, something like

aCollection.doEachBulk(100) { bulk -> 
    ...
}

Is there ?

Upvotes: 1

Views: 100

Answers (1)

epidemian
epidemian

Reputation: 19219

I'm not sure if I understand your question correctly, but you may be looking for the collate method.

(1..20).collate(5).each { subRange ->
    println subRange
}

Output:

[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]

Upvotes: 4

Related Questions