Monday, August 15, 2011

Basic predicate functions

After discussing sets and predicates in my previous post I have come up with some basic predicate functions:

(def every-true? (partial every? true?))
(def some-true? (comp not nil? (partial some #{true})))

(defn union
  [& predicates]

  (fn [& obj]
    (every-true? (map (fn [predicate] (apply predicate obj)) predicates))))

(defn intersection
  [& predicates]

  (fn [& obj]
    (some-true? (map (fn [predicate] (apply predicate obj)) predicates))))

(defn cartesian-product
  [& predicates]

  (fn [& obj]
    (and
     (= (count obj) (count predicates))
     (every-true?
      (map
       (fn [i]
         (apply (nth predicates i) (nth obj i)))
       (range 0 (count obj)))))))

No comments:

Post a Comment