Huh, didn't know that about 'distinct'. Thank you for the tip. On Nov 27, 9:18 pm, Ken Wesson <kwess...@gmail.com> wrote: > On Sat, Nov 27, 2010 at 11:03 PM, Benny Tsai <benny.t...@gmail.com> wrote: > > 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)))) > > While (distinct foo) is technically lazy, it retains in a set in > memory every distinct element seen so far, and with the indices here > that's as bad as holding onto the head. And all to avoid a 0 being > repeated at the start. Try: > > (defn add-zero [coll] > (if (= 0 (first coll)) coll (cons 0 coll))) > > (defn split-when [pred coll] > (if (empty? coll) > '() > (let [indices (add-zero (index-filter pred coll)) > index-groups (partition-all 2 1 indices)] > (map #(apply subsequence coll %) index-groups))))
-- 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 Note that posts from new members are moderated - please be patient with your first post. 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