On Fri, Dec 19, 2008 at 4:32 PM, Rich Hickey <richhic...@gmail.com> wrote:
>
> 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)

hmmm...  if I do this:

user=> (partition 2 1 (iterate inc 1)) (.printStackTrace *e)

it ends like this:

[...]
57) (587257 587258) (587258 587259) (587259 587260) (587260 587261)
(587261 587262) (587262 587263) (587263 587264) (587264 587265)
(587265 587266) (587266 587267) (587267 587268) (587268 587269)
(587269 587270) (587270 587271) (587271 587272) (587272 587273)
(587273 587274) user=> java.lang.OutOfMemoryError: Java heap space
        at 
clojure.lang.PersistentHashMap$BitmapIndexedNode.assoc(PersistentHashMap.java:419)
        at clojure.lang.PersistentHashMap.assoc(PersistentHashMap.java:139)
        at clojure.lang.PersistentHashMap.assoc(PersistentHashMap.java:52)
        at clojure.lang.Var.pushThreadBindings(Var.java:264)
        at clojure.core$print_sequential__4821.invoke(core_print.clj:37)
        at clojure.core$fn__4889.invoke(core_print.clj:133)
        at clojure.lang.MultiFn.invoke(MultiFn.java:152)
        at clojure.core$pr_on__3671.invoke(core.clj:1742)
        at clojure.core$print_sequential__4821.invoke(core_print.clj:54)
        at clojure.core$fn__4889.invoke(core_print.clj:133)
        at clojure.lang.MultiFn.invoke(MultiFn.java:152)
        at clojure.core$pr_on__3671.invoke(core.clj:1742)
        at clojure.lang.Var.invoke(Var.java:331)
        at clojure.lang.RT.print(RT.java:1200)
        at clojure.lang.Repl.main(Repl.java:94)
nil

This is Ubuntu 7.10 using:
$ java -version
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)
Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode, sharing)

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