|P is for Practical|
Re: Rosetta code: Split an array into chunksby moritz (Cardinal)
|on Sep 25, 2010 at 10:15 UTC||Need Help??|
Here is a Perl 6 solution in a single statement. It's probably not the best solution, but it does work today in rakudo:
The 0, $len ...^ *>=@l creates a list in steps of $len, up to (but excluding) the number of elements in @l.
Inside the map there is an array slice. The min @l.end is only necessary because Rakudo doesn't clip array slices to the end of the list yet (which is a known and reported bug).
I'm still looking for a nicer solution, will update my post if I find one.
Update: Nicer solution:
This creates an infinite list (' ', ' ', "\n", ' ', ' ', "\n", ...), and then zips the input list with it. (zip = takes one item from each list in turn). Zips stops when the shortest list is exhausted, so we don't have to worry about it looping forever.
The result is then joined together, and the last separator is unconditionally substituted for a newline.
Perl 6 - links to (nearly) everything that is Perl 6.