On Sun, Nov 28, 2010 at 5:23 PM, Asim Jalis <asimja...@gmail.com> wrote: > On Sun, Nov 28, 2010 at 1:17 PM, Ken Wesson <kwess...@gmail.com> wrote: >> The implementation I posted earlier appears to fit the contract of >> your partition-before (and, additionally, works properly if the input >> starts with (nil ...) :)). > > Good catch :) I should have used (empty? coll) instead of (nil? > first-item) to check for emptiness. > > The implementation you posted earlier (split-when) is quite elegant. > However, one thing I noticed about it was that it calls pred multiple > times for each item in coll -- it would be nice if this redundancy > could be removed somehow.
I don't think it does -- it calls clojure.core/split-with, which presumably runs over the items until it hits one for which pred was false (so, ipred is true) and splits the collection there. Only the second half gets another split-with called on it, and with the first item (which was already tested) omitted. If split-with is actually implemented as simply [(take-while pred coll) (drop-while pred coll)] then the redundancy exists but your issue is with the authors of split-with. :) My partition-dropping does call pred twice on some items, specifically the first item in each partition gets an extra pred check to see if it should be dropped. In both cases I'd probably actually use a direct implementation in terms of lazy-seq in production code, which would get rid of any such redundancies, get rid of the overhead of wrapping pred in ipred, and get rid of the risk of a stack overflow from piling on nested lazy calls (which may or may not actually be there, mind you). -- 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