Hi Bill,
How about something like...
(filter #(every? true? ((juxt f g h) %)) my-list)
In the character sense it's not more terse than your original example,
but it is a little nicer to add additional functions to compare with.
Throwing it in a function could be useful too
(defn multi-filter
[coll & funcs]
(filter #(every? true? ((apply juxt funcs) %)) coll))
=> (multi-filter [0 1 2 3 4] even? (complement zero?))
=> (2 4)
On Jul 23, 6:33 pm, ataggart <[email protected]> wrote:
> Not that preserves the short-circuiting behavior of 'and. This works
> though:
>
> (defmacro andf [& fns]
> (let [x# (gensym)]
> `(fn [~x#] (and ~@(map #(list % x#) fns)))))
>
> user=> (filter (andf integer? odd?) [1 2 3.1])
> (1)
>
> On Jul 23, 2:27 pm, ".Bill Smith" <[email protected]> wrote:
>
>
>
> > I want to filter a list based on the logical AND of a set of
> > predicates. For example, let's say I have single-argument functions
> > f, g, and h (without side-effects), and I want those items x in the
> > list such that (f x), (g x) and (h x) are all true. I know I can do
> > this:
>
> > (filter #(and (f %) (g %) (h %)) my-list)
>
> > Is there a more terse way to express the same thing? I suspect there
> > is a mechanism similar to (or perhaps a generalization of) composition
> > for that, but I couldn't find it in the API docs.
>
> > Bill Smith
> > Austin, TX
--
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