# Incremental Array Clumping

Hello friends,

I’m stuck on a problem and am hoping one of you will be able to show me a solution. Given an Array, I am trying produce a new Array containing several sub-Arrays consisting of the original Array .clumped at various scales, i.e. an Array with clumps of 3 elements, another with clumps of 5 elements, etc. Furthermore, I need the clumps to be windowed element-wise rather than clumping non-overlapping contiguous sequences of elements, so that given

``````~clumps = [1, 3, 4, 6];
~array = (1..10)
``````

I would hope to end up with

``````[[[1], [2], [3],...[10]], [[1, 2, 3], [2, 3, 4], [3, 4, 5],...[8, 9, 10]], [[1, 2, 3, 4], [2, 3, 4, 5],...[7, 8, 9, 10]], [[1, 2, 3, 4, 5, 6],...[5, 6, 7, 8, 9, 10]]]
``````

Note that the actual Array I’m trying to process doesn’t consist of sequential integers; I’m just using those here for illustrative purposes.

-Boris

You are looking for Array.slide!

`clumps.collect { |clump| array.slide(clump) }`

1 Like

There it is! And beautifully compact.
The actual code in this case would be

``````~clumps.collect{|clump| ~array.slide(clump, 1).clump(clump)}
``````

since .slide expands the original Array but doesn’t do any clumping.
Thanks, V-dog!

1 Like