On Fri, Dec 19, 2008 at 1:02 PM, hosia...@gmail.com <hosia...@gmail.com> wrote:
>
> 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)))))

There is a function called partition in Clojure's core.clj that does
this, except it does not pad, but rather discards any incomplete
groups.  It's recursive, though, so it runs out of heap on large
sequences like (iterate inc 1).

> user=> (groups-of 2 [1 2 3 4])
> ((1 2) (3 4))

(partition 2 [1 2 3 4])
=> ((1 2) (3 4))

> user=> (groups-of 3 [1 2 3 4] "*")
> ((1 2 3) (4 "*" "*"))

(partition 3 [1 2 3 4])
=> ((1 2 3))

> 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 ?

First you will need to send in a Contributor Agreement.
http://clojure.org/contributing

> 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 ?

There is no such thing at the moment although it's been mentioned in the past.

-- 
Michael Wood <esiot...@gmail.com>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to