Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'Slice' a Collection in Groovy

Tags:

groovy

I have a collection of objects that I want to break up into a collection of collections, where each sequential group of 3 elements is in one collection. For example, if I have

def l = [1,4,2,4,5,9]

I want to turn this into:

def r = [[1,4,2], [4,5,9]]

I'm doing it now by iterating over the collection and breaking it up.. but I then need to pass those 'groups' into a parallelized function that processes them.. It would be nice to eliminate this O(n) pre-processing work and just say something like

l.slice(3).collectParallel { subC -> process(subC) }

I've found the step method on the Range class, but it looks like that only acts on the indices. Any clever ideas?

Update: I don't think this is a duplicate of the referenced link, although it's very close. As suggested below, it's more of the iterator-type thing I'm looking for.. the sub-collections will then be passed into a GPars collectParallel. Ideally I wouldn't need to allocate an entire new collection.

like image 599
Bobby Avatar asked May 03 '11 17:05

Bobby


People also ask

How do I split a List in Groovy?

Groovy adds a split() method to objects with a closure parameter. In the closure we must define a condition to split up the items in two lists. One list contains all the items that apply to the closure condition, the other list contains the rest of the items that don't apply to the closure condition.

How do I cut a string in Groovy?

The Groovy community has added a take() method which can be used for easy and safe string truncation. Both take() and drop() are relative to the start of the string, as in "take from the front" and "drop from the front". Save this answer.


1 Answers

Check out groovy 1.8.6. There is a new collate method on List.

def list = [1, 2, 3, 4]
assert list.collate(4) == [[1, 2, 3, 4]] // gets you everything   
assert list.collate(2) == [[1, 2], [3, 4]] //splits evenly
assert list.collate(3) == [[1, 2, 3], [4]] // won't split evenly, remainder in last list.

Take a look at the Groovy List documentation for more info because there are a couple of other params that give you some other options, including dropping the remainder.

As far as your parallel processing goes, you can cruise through the lists with gpars.

def list = [1, 2, 3, 4, 5]
GParsPool.withPool {
  list.collate(2).eachParallel {
     println it
  }
}
like image 150
benkiefer Avatar answered Oct 12 '22 09:10

benkiefer