How do you compose two or more predicates in clojure?

Related searches

I want to apply two (or more) predicates to a single value. For example, say I want to test if a value is a positive integer:

(defn posint? [n]
   (and (integer? n) (pos? n)))

That does it, but what if I want to compose the predicates applied? I don't want to write a function for each possible combination of predicates.

In Clojure 1.3 there is actually a built-in function called every-pred to do just this. See here.

(defn posint? [n]
  ((every-pred integer? pos?) n))

comp - clojure.core, the sort order is that the lower price is superior to the higher one and if the We need an example that composes more than just two functions. ; The Takes a set of predicates and returns a function f that returns true if all of its composing� Step (arity 2) - this is a standard reduction function but it is expected to call the rf step arity 0 or more times as appropriate in the transducer. For example, filter will choose (based on the predicate) whether to call rf or not. map will always call it exactly once. cat may call it many times depending on the inputs.

If you're looking to do these checks inline, the following may be what you are looking for.

(defn predicate-test [fns t]
    "test all predicates against value t and return true iff all predicates return true."
    (every? true? ((apply juxt fns) t)))

(predicate-test [integer? pos?] 4)

You could then create named versions for your most used predicate tests:

(def posint? (partial predicate-test [integer? pos?])

(posint? 4)

some-fn - clojure.core, Takes a set of predicates and returns a function f that returns the first logical true value returned by one of its composing predicates against any of its values in a given coll satisfy some predicate), but have more than ;; one predicate. Note the double parentheses at the ;; beginning: because some-fn returns a function,� Clojure macros apply to the result of the clojure reader's parsing of the source text. Has two effects: You will only get valid clojure datastructures as arguments to a macro. You can only output one valid clojure datastructure from a macro. One of the consequences of this is that certain

If you want to compose them in an and relationship, use every-pred:

((every-pred pos? even?) 5)
;false
((every-pred pos? even?) 6)
;true
((every-pred pos? even?) -2)
;false

And if you want to compose them in an or relationship, use some-fn:

((some-fn pos? even?) 5)
;true
((some-fn pos? even?) 6)
;true
((some-fn pos? even?) -2)
;true
((some-fn pos? even?) -3)
;false

every-pred - clojure.core, Takes a set of predicates and returns a function f that returns true if all of its composing predicates return a logical true value against all of its arguments, else it� my_all_succeed_naive ( [multiple_of_three, multiple_of_five], 5) # => false. This looks like this works, as we get 15 is a multiple of 3 and a multiple of 5, but we still check if the item is a multiple of 5, even if our multiple of 3 check failed.

filtering with multiple predicates, 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 <peter.schul. One special consideration however is that you must keep in mind that you are receiving code as data, not evaluated arguments, and that you are most commonly producing new code as data, so often it’s not helpful to spec the :ret value of a macro (as it’s just code).

spec Guide, Sets can also be used as predicates that match one or more literal values: The s/merge spec can be used to combine multiple s/keys specs into a single spec� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

It seems when you have: (defn dev? ) and (defn dev ) - which are clearly two different functions, and if the cursor at dev, dumb-jump-go would prefer to jump to (defn dev?) - the wrong one. That I guess happens because dev? is at t

Comments
  • This one even handles (posint? "a")! I am in need of a 1.2 solution though unfortunately.
  • @stand: I think, that you can take this function from Clojure sources and put into your code
  • Ah yes, it was the juxt function that I was missing. Thanks!
  • ...though this solution throws an exception when you do a (posint? "a") since pos? throws a CastException. I can't think of a way to short-circuit the way and does.