(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'.
http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/split-with
(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
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>
On Jan 21, 8:01 pm, Dan Schmidt <dan.schm...@gmail.com> 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
>
(defn combine-preds
[& preds]
(fn [& args] (every? #(apply % args) preds)))
then filter files like
(filter (combine-preds pred1 pred2) (file-seq in-dir))
Perry
(defn every-pred?
"Mimics AND"
[& preds]
(fn [& args] (every? #(apply % args) preds)))
(defn any-pred?
"Mimics OR"
[& preds]
(fn [& args] (some #(apply % args) preds)))
They're very handy, though. Makes it easy to create a complex
predicate from simple ones.
Sean
I've noticed that most uses of this kind of thing are for fixed sets
of predicates, and almost always for two, so I'd define:
(defmacro both [p1 p2]
`(fn [& args#]
(and (apply ~p1 args#)
(apply ~p2 args#))))
Which reads nicely:
(filter (both number? even?) (range 1 7))
and is probably more efficient (not that I've tested it).
You can generalize this to `all`, of course.
every? does short-circuit.
user=> (every? #(% 5)
(list
(fn [a] (println "First a: " a) false)
(fn [a] (println "Second a: " a) true)))
First a: 5
false
--
On Jan 22, 6:42 am, David Nolen <dnolen.li...@gmail.com> wrote:
> Oops you're right :) And better written as well :D
>
> On Fri, Jan 22, 2010 at 1:26 AM, Richard Newman <holyg...@gmail.com> wrote:
> > I agree, one possible improvement would be short circuiting on the first
> >> false pred.
>
> > every? does short-circuit.
>
> > user=> (every? #(% 5)
> > (list
> > (fn [a] (println "First a: " a) false)
> > (fn [a] (println "Second a: " a) true)))
>
> > First a: 5
> > false
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clo...@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+u...@googlegroups.com<clojure%2Bunsu...@googlegroups.com>