I'm learning Clojure by trying to implement some functions from Ruby
core/stdlib/ActiveSupport's core_ext.
The first one I wrote is groups-of (similar to ActiveSupport's
in_groups_of):
(defn groups-of
"Returns coll in groups of x size, optionally padding any remaining
slots with specified value"
([x coll]
(loop [list coll result nil]
(if (empty? list)
result
(recur (drop x list) (concat result [(take x list)])))))
([x coll padding]
(groups-of x (concat coll (replicate (rem (- x (rem (count coll)
x)) x) padding)))))
user=> (groups-of 2 [1 2 3 4])
((1 2) (3 4))
user=> (groups-of 3 [1 2 3 4] "*")
((1 2 3) (4 "*" "*"))
I've got a few questions:
1. Can the code above be improved ? I'm not sure if using concat is
the best way to append to a collection.
2. If I wanted to extend the function to support executing a piece of
code for each group would it be recommended to write a macro to
support it (by passing an optional body for example) or rather use
apply on the returned sequence ? How would an example macro look
like ?
3. How to contribute to clojure-contrib ?
4. Is there a package retrieval/dependency tracking system for Clojure
(sth. similar to Rubygems / asdf / apt-get etc.) or is someone working
on such a system ?
Thanks
--
Karol Hosiawa
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---