Re: filtering with multiple predicates

2010-07-24 Thread Cameron
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

Re: filtering with multiple predicates

2010-07-24 Thread Mark Triggs
j-g-faustus writes: > I was apparently too late. Oh well :) My apologies for pre-emptively stealing your idea :) -- Mark Triggs -- 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 th

Re: filtering with multiple predicates

2010-07-24 Thread j-g-faustus
On Jul 24, 8:53 am, Mark Triggs wrote: > I cheated and used 'every?' :) I was apparently too late. Oh well :) -- 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 memb

Re: filtering with multiple predicates

2010-07-24 Thread Meikel Brandmeyer
Am 24.07.2010 um 08:23 schrieb Meikel Brandmeyer: > (defn and-pred > [& preds] > (if-let [preds (seq preds)] >(fn [x] > (seq (drop-while identity (map #(% x) preds >(constantly true))) Ok. Switch brain on. Second try. (defn and-pred [& preds] (fn [x] (not (seq (drop-w

Re: filtering with multiple predicates

2010-07-24 Thread j-g-faustus
On Jul 23, 11:27 pm, ".Bill Smith" wrote: > (filter #(and (f %) (g %) (h %)) my-list) Here's another one, just to add to the collection: (defn andp [& fns] (fn [& args] (every? #(apply % args) fns))) (filter (andp f g h) my-list) -- You received this message because you are subscribed t

Re: filtering with multiple predicates

2010-07-23 Thread Mark Triggs
I cheated and used 'every?' :) (defn andf [& preds] (fn [& args] (every? #(apply % args) preds))) ".Bill Smith" writes: > 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-

Re: filtering with multiple predicates

2010-07-23 Thread Meikel Brandmeyer
Hi, Am 23.07.2010 um 23:27 schrieb .Bill Smith: > (filter #(and (f %) (g %) (h %)) my-list) Here a completely different approach. Note, it also preserves short-circuiting. (defn and-pred [& preds] (if-let [preds (seq preds)] (fn [x] (seq (drop-while identity (map #(% x) preds

Re: filtering with multiple predicates

2010-07-23 Thread David Cabana
Here's an approach that doesn't use macros. I'm not sure I'd actually do this, but here it is just for grins. (defn bind [pred] (fn [[x bool]] [x (and (pred x) bool)])) (defn comp# [ & preds ] (let [ phi (reduce comp (map bind preds))] (fn [x] (second (phi [x true]) user> (filter (c

Re: filtering with multiple predicates

2010-07-23 Thread David Cabana
Alex, Very slick; I really liked that. Thank you for posting it . David On Fri, Jul 23, 2010 at 6:33 PM, ataggart 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#) f

Re: filtering with multiple predicates

2010-07-23 Thread ataggart
Quite right! Being a variation on the actual implementation of the 'and macro, it's probably easier to understand than my cobbled together version (needing a gensym should have been a giveaway). On Jul 23, 4:07 pm, ".Bill Smith" wrote: > For that matter, I could do this: > > (defmacro andp >   (

Re: filtering with multiple predicates

2010-07-23 Thread .Bill Smith
For that matter, I could do this: (defmacro andp ([] true) ([x p] `(~p ~x)) ([x p & next] `(let [and# (~p ~x)] (if and# (andp ~x ~...@next) and# On Jul 23, 5:33 pm, ataggart wrote: > Not that preserves the short-circuiting behavior of 'and.  This works > though: > > (defm

Re: filtering with multiple predicates

2010-07-23 Thread ataggart
The problem is that 'comp passes the result of one function as the arg of the next, which isn't likely to be applicable for predicate functions, nor will it compose with the 'and macro. On Jul 23, 3:28 pm, Peter Schuller wrote: > > Is there a more terse way to express the same thing?  I suspect t

Re: filtering with multiple predicates

2010-07-23 Thread ataggart
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" wrote: > I want to filter a list base

Re: filtering with multiple predicates

2010-07-23 Thread Peter Schuller
> 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. http://stackoverflow.com/questions/2822140/function-composition-clojure -- / Peter Schuller -- Yo

filtering with multiple predicates

2010-07-23 Thread .Bill Smith
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 %)