On Dec 19, 8:59 am, "Michael Wood" <esiot...@gmail.com> wrote:
> 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).
>
That's not true - partition is lazy:
;with -Xmx128M
(last (partition 2 (take 100000000 (iterate inc 1))))
-> (99999999 100000000)
Rich
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---