Hmm, on the same (micro admittedly) benchmark as above...

(time (let [[a b] (unzip-with even? (range 100000))] [(nth a 49999)
(nth b 49999)]))
"Elapsed time: 177.797 msecs"
[99998 99999]

that's a bit slower than both the previous versions. The reduce
version does only apply the pred once per item I think?


On Sun, Mar 8, 2009 at 12:37 PM, Christophe Grand <christo...@cgrand.net> wrote:
>
> The question showed up the other day on #clojure with the additional
> constraint to evaluate pred only once per item, here is Rich's solution:
>
> http://paste.lisp.org/display/76458#1
>
> (defn unzip-with [pred coll]
>  (let [pvs (map #(vector (pred %) %) coll)]
>    [(for [[p v] pvs :when p] v)
>     (for [[p v] pvs :when (not p)] v)]))
>
>
> Christophe
>
> David Sletten a écrit :
>> I'm reading the Sequences chapter of Programming Clojure, and Stu
>> points out that split-with combines the semantics of take-while and
>> drop-while. But is there a function that does something similar with
>> "filter"? Namely, rather than simply filtering the elements of a
>> collection that satisfy a predicate I also want to capture those that
>> don't. Something like this:
>> (defn filter-split [pred coll]
>>    (loop [trues '() falses '() coll coll]
>>      (cond (empty? coll)
>>            (vector (reverse trues) (reverse falses))
>>            (pred (first coll))
>>            (recur (cons (first coll) trues) falses (rest coll))
>>            :else
>>            (recur trues (cons (first coll) falses) (rest coll)))))
>>
>> (filter-split #{\a\e\i\o\u} "is this not pung?") => [(\i \i \o \u)
>> (\s \space \t \h \s \space \n \t \space \p \n \g \?)]
>> (filter-split even? (range 10)) => [(0 2 4 6 8) (1 3 5 7 9)]
>>
>> Aloha,
>> David Sletten
>>
>> >
>>
>>
>
>
> --
> Professional: http://cgrand.net/ (fr)
> On Clojure: http://clj-me.blogspot.com/ (en)
>
>
>
> >
>

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