partition blew up the stack

9 views
Skip to first unread message

Christophe Grand

unread,
Jun 10, 2008, 3:29:31 AM6/10/08
to clo...@googlegroups.com
user=> (dorun (partition 3 (range 100000)))
java.lang.StackOverflowError

It's because of the take-while which is indirectly called at each lazy-cons.
Here is an easy fix:

(defn partition
"Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
do not overlap."
([n coll]
(partition n n coll))
([n step coll]
(take-while #(= n (count %))
((fn this [coll]
(when (seq coll)
(lazy-cons (take n coll) (this (drop step coll))))) coll))))

Or, maybe better, to get rid of the take-while entirely:
(defn partition
"Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
do not overlap."
([n coll]
(partition n n coll))
([n step coll]
(let [hd (take n coll)]
(if (= n (count hd))
(lazy-cons hd (partition n step (drop step coll)))))))


Christophe

Rich Hickey

unread,
Jun 10, 2008, 7:21:01 AM6/10/08
to Clojure


On Jun 10, 3:29 am, Christophe Grand <christo...@cgrand.net> wrote:
> user=> (dorun (partition 3 (range 100000)))
> java.lang.StackOverflowError
>
> It's because of the take-while which is indirectly called at each lazy-cons.

Fixed - thanks for the report.

Rich
Reply all
Reply to author
Forward
0 new messages