I agree, one possible improvement would be short circuiting on the first
false pred.
(defn combine-preds [pred & others]
(fn [v]
(loop [result true pred pred others others]
(if (not result)
result
(if (nil? pred)
result
(recur (pred v) (first others) (rest others)))))))
(defn mod2 [n]
(zero? (mod n 2)))
(defn mod3 [n]
(zero? (mod n 3)))
((combine-preds mod2 mod3) 6) ; true, checks both
((combine-preds mod2 mod3) 9) ; false, never gets past first pred
On Thu, Jan 21, 2010 at 3:40 PM, Perry Trolard <[email protected]> wrote:
> I think it's easier to think about combining predicates separately
> from your file-filtering code. I'd use a higher-order function like
> the following
>
> (defn combine-preds
> [& preds]
> (fn [& args] (every? #(apply % args) preds)))
>
> then filter files like
>
> (filter (combine-preds pred1 pred2) (file-seq in-dir))
>
> Perry
>
>
>
> On Jan 21, 2:08 pm, nwalex <[email protected]> wrote:
> > Brilliant. Thanks Dan, that did the trick.
> >
> > On Jan 21, 8:01 pm, Dan Schmidt <[email protected]> wrote:
> >
> >
> >
> > > I don't know how efficient it is (or how idiomatic it really is), but
> > > this should work:
> >
> > > (defn find-files
> > > "Find files in directory that match predicates pred & others"
> > > [in-dir pred & others]
> > > (reduce (fn [xs f] (filter f xs)) (file-seq in-dir) (cons pred
> others)))
> >
> > > Dan
> >
> > > On Thu, Jan 21, 2010 at 2:47 PM, nwalex <[email protected]>
> wrote:
> > > > I'm very new to Clojure, and to functional programming in general.
> Can
> > > > anyone tell me the idiomatic way to implement this function?
> >
> > > > (defn find-files
> > > > "Find files in directory that match predicates pred & others"
> > > > [in-dir pred & others]
> > > > (filter pred (file-seq in-dir)))
> >
> > > > What is the best way to filter a sequence using multiple predicates?
> I
> > > > want to return a sequence containing the files that match predicate
> > > > pred and all other predicates bound to 'others'.
> >
> > > > --
> > > > 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]<clojure%[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 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]<clojure%[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 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