I implemented something like that. my goal was to avoid the predicate
being called twice, but apart from that it has some overhead.


(defn- apl-compr-
  "the apl compress operator:  0 1 1 0 1 / 1 2 3 4 5 -> 2 3 5
  implemented in clojure (apl-compr- [false true true false true] '(1
2 3 4 5)) -> (2 3 5)"
  [bcoll coll mod]
  (lazy-seq
    (loop [b (seq bcoll) s (seq coll)]
      (when (and b s)
        (if (mod (first b))
          (cons (first s) (apl-compr- (rest b) (rest s) mod))
          (recur (next b) (next s)) )))))

(defn part1
  [f s]
  (let [m (map f s)]
    [(apl-compr- m s identity) (apl-compr- m s not)] ))


There is also a similar function in erlang, partition, but it is eager
and recursive, so not really suited for clojure



On 23 Maj, 21:56, B Smith-Mannschott <bsmith.o...@gmail.com> wrote:
> On Sun, May 23, 2010 at 21:21, Michael Gardner <gardne...@gmail.com> wrote:
> > I need to use a predicate to divide one list into two, one containing all 
> > values for which the predicate is true, and the other with all remaining 
> > values. I can do this easily by filtering twice, once with the predicate 
> > and once with its complement, but is there some core or contrib function 
> > that will do this more directly and efficiently? I'm not sure what the best 
> > way to search for something like this would be.
>
> Yes and No.
>
> Yes: such a function exists: clojure.contrib.seq-utils/separate
>
> No: It's not as efficient as you'd like. It does just what you
> describe in your post: it filters twice.
>
> (defn separate
>   "Returns a vector:
>    [ (filter f s), (filter (complement f) s) ]"
>   [f s]
>   [(filter f s) (filter (complement f) s)])
>
> // Ben
>
> --
> 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 
> athttp://groups.google.com/group/clojure?hl=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
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

Reply via email to