Adapting my answer to this question on partitioning a List, I came up with this method:
Map.metaClass.partition = { size ->
def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [:] : ret
}
rslt.last() ? rslt : rslt[ 0..-2 ]
}
So if you take this map:
def origMap = [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f']
All of the following assertions pass :-)
assert [ [1:'a'], [2:'b'], [3:'c'], [4:'d'], [5:'e'], [6:'f'] ] == origMap.partition( 1 )
assert [ [1:'a', 2:'b'], [3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 2 )
assert [ [1:'a', 2:'b', 3:'c'], [4:'d', 5:'e', 6:'f'] ] == origMap.partition( 3 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 4 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e'], [6:'f'] ] == origMap.partition( 5 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f'] ] == origMap.partition( 6 )
Or, as a Category
(to avoid having to add anything to the metaClass
of Map
:
class MapPartition {
static List partition( Map delegate, int size ) {
def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [:] : ret
}
rslt.last() ? rslt : rslt[ 0..-2 ]
}
}
Then, where you need this functionality, you can simply use
the Category like so:
use( MapPartition ) {
assert [ [1:'a'], [2:'b'], [3:'c'], [4:'d'], [5:'e'], [6:'f'] ] == origMap.partition( 1 )
assert [ [1:'a', 2:'b'], [3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 2 )
assert [ [1:'a', 2:'b', 3:'c'], [4:'d', 5:'e', 6:'f'] ] == origMap.partition( 3 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 4 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e'], [6:'f'] ] == origMap.partition( 5 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f'] ] == origMap.partition( 6 )
}