Christian Romney <[email protected]> writes:
Hey Christian,
> (defn segregate
> "Takes a predicate, p, and a collection, coll, and separates the items in
> coll
> into matching and non-matching subsets. Like Scheme or Ruby's partition."
> [p coll]
> (loop [s coll y [] n []]
> (if (empty? s) [y n]
> (if (p (first s))
> (recur (rest s) (conj y (first s)) n)
> (recur (rest s) y (conj n (first s)))))))
>
> If not, I have two follow-up questions.
No, I think there's nothing like that in core. But I also feel a need
for that, since I also frequently do
(let [e (filter even? coll)
o (remove even? coll)]
...)
which is nearly equivalent, except that it's lazy (i.e., e and o are
lazy seqs) where yours is eager (returns two vectors). group-by in core
is also eager.
> 1) Is there a better way to write segregate
This would work and also returns a vector of two lazy seqs:
(def segregate (juxt filter remove))
Then you can do
(let [[e o] (segregate even? coll)]
...)
exactly as with your version.
> 2) Is this useful enough to consider adding to core?
IMHO, yes, but it should return a vector of 2 lazy seqs. The eager case
is captured enough by group-by, IMHO.
However, the what's bad with the juxt approach above is that the
collection is iterated twice. If somebody comes up with a version that
returns a vector of two lazy seqs and which iterates the input
collection only once, she'd get my warmest thank you, and my vote for
putting it into core.
Bye,
Tassilo
--
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.