That's what I get for coding without taking my afternoon nap :)
Try 3. Tweaked 'subsequence' and 'split-when' so that it is no longer
necessary to calculate the index for the end of the collection. Also
added a check to 'split-when' to return an empty list when called on
an empty collection; otherwise it would return (()), which seemed like
a strange result.
(use '[clojure.contrib.seq :only (indexed)])
(defn index-filter [pred coll]
(when pred
(for [[idx elt] (indexed coll) :when (pred elt)] idx)))
(defn subsequence
([coll start]
(drop start coll))
([coll start end]
(take (- end start) (drop start coll))))
(defn split-when [pred coll]
(if (empty? coll)
'()
(let [indices (distinct (conj (index-filter pred coll) 0))
index-groups (partition-all 2 1 indices)]
(map #(apply subsequence coll %) index-groups))))
On Nov 27, 7:26 pm, Ken Wesson <[email protected]> wrote:
> On Sat, Nov 27, 2010 at 8:35 PM, Benny Tsai <[email protected]> wrote:
> > Here's a fixed version that...
>
> > 1. Is lazy everywhere.
> > 2. No longer loses elements :)
>
> ...
>
> > (defn split-when [pred coll]
> > (let [coll-end (count coll)
>
> Er, did you just say "is lazy everywhere"? :)
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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