Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Rosetta code: Split an array into chunks

by stefan k (Curate)
on Sep 27, 2010 at 08:42 UTC ( #862165=note: print w/replies, xml ) Need Help??

in reply to Rosetta code: Split an array into chunks

(defn pm-chunk [data chsize] (apply str (interleave (map #(apply str (interpose " " %)) (partition chsize chsize nil data)) (repeat \newline))))
Then use it at the REPL:
user=> (print (pm-chunk ["a" "bb" "c" "d" "e" "f" "g" "h"] 3)) a bb c d e f g h
Please note that the rather ugly "apply str" at the top is necessary to fulfil the perlish requirements. Usually you'd stop at creating the right sequence, which is achieved after the call to interleave.

Regards... stefan k
you begin bashing the string with a +42 regexp of confusion

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://862165]
[shmem]: yes, at least for the result in @foo
[shmem]: but the results of both operations are different.
[shmem]: result := the discarded result (what's leftmost of the statement)
[shmem]: push returns the number of elements in @foo, the assignment returns all the elements in the (renewed) @foo
[Lady_Aleena]: Sanity check please? push @line, ref($list_addition ) eq 'ARRAY' ? @$list_addition : $list_addiction; # so I don't have the change existing uses of the sub.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-04-27 11:38 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (503 votes). Check out past polls.