Back
every-pred (clj)
(source)function
(every-pred p)
(every-pred p1 p2)
(every-pred p1 p2 p3)
(every-pred p1 p2 p3 & ps)
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 returns
false. Note that f is short-circuiting in that it will stop execution on the first
argument that triggers a logical false result against the original predicates.
Examples
clojure/core.typed
(ns clojure.core.typed.test.some-fn
(:require [clojure.core.typed :as t]))
(let [s (t/ann-form (every-pred symbol? number?) (predicate (I Number t/AnyInteger)))]
(t/print-env "f"))
clojure/core.typed
(ns clojure.core.typed.test.pred
(:require
[clojure.test :refer :all]
[clojure.core.typed :as t]))
(deftest hmap-pred-test
(is ((every-pred
(t/pred
(t/HMap)))
{}
{:a 'blah}))
(is ((complement
(t/pred
(t/HMap :mandatory {:a Number})))
{}))
(is ((t/pred
(t/HMap :mandatory {:a Number}))
{:a 1}))
(is ((every-pred
(t/pred
(t/HMap :optional {:a Number})))
{:a 1}
{}
{:b 'a}))
(is ((every-pred
(t/pred
(t/HMap :absent-keys #{:a})))
{:b 'a}))
(is (not
((every-pred
(t/pred
(t/HMap :absent-keys #{:a})))
{:a 'a})))
)
(deftest hvec-pred-test
(is ((t/pred '[Number Number])
[1 2]))
(is ((every-pred
(complement
(t/pred '[Number Number])))
['a 2]
[]
[1]
[1 2 3]))
(is ((every-pred
(t/pred
'[Number Number Number *]))
[1 2]
[1 2 3]
[1 2 3 4 5 6 6 7 4 2 1]))
(is ((every-pred
(complement
(t/pred
'[Number Number Number *])))
[]
[1]
[1 2 'a]
[1 2 3 4 5 'a 6 7 4 2 1])))
(deftest rec-pred-test
(is ((every-pred
(t/pred (t/Rec [x] (t/U '[x] Number))))
1
'[1]
'[[1]]
'[[[[[2.2]]]]]))
(is ((every-pred
(t/pred (t/Rec [x] (t/U '{:a x} Number))))
1
'{:a 1}
'{:a {:a 1}}
'{:a {:a {:a {:a {:a 1}}}}}))
(is ((every-pred
(complement
(t/pred (t/Rec [x] (t/U '[x] Number)))))
'[1 1]
'[[1] [1]])))
(deftest singleton-pred-test
(is ((t/pred true)
true))
(is ((t/pred (t/Value true))
true))
(is ((t/pred false)
false))
(is ((t/pred (t/Value false))
false))
(is ((t/pred 'sym)
'sym))
(is ((t/pred (t/Value sym))
'sym))
(is ((t/pred ':sym)
':sym))
(is ((t/pred (t/Value :sym))
':sym))
(is ((t/pred nil)
nil))
(is ((t/pred (t/Value nil))
nil))
(is ((t/pred '1)
1))
(is ((every-pred
(complement
(t/pred '1)))
1.0))
(is ((t/pred (t/Value 1))
1)))
(deftest countrange-pred-test
(is ((every-pred
(t/pred (t/CountRange 0)))
nil
[]
{}
'()))
(is ((every-pred
(complement
(t/pred (t/CountRange 0))))
; only supports clojure collections
(into-array [])
)))
(deftest intersect-pred-test
(is ((every-pred
(t/pred (t/I Number Long)))
1))
(is ((every-pred
(complement
(t/pred (t/I Number Long))))
1.1))
(is ((every-pred
(complement
(t/pred (t/I Number Long))))
1.1)))
(deftest union-pred-test
(is ((every-pred
(t/pred (t/U Number Long)))
1
1.1))
(is ((every-pred
(complement
(t/pred (t/U Number Long))))
'a))
(is ((every-pred
(complement
(t/pred t/Nothing))
(complement
(t/pred (t/U))))
'a)))
(deftest tfn-name-test
(is ((every-pred
(t/pred (clojure.core.typed/Option Number)))
nil
1
1.1)))
(deftest iseq-pred-test
(is ((every-pred
(t/pred (t/Seq Number)))
'(1 2)))
(is ((every-pred
(complement
(t/pred (t/Seq Number))))
[1 2])))
(deftest any-pred-test
(is ((every-pred
(t/pred t/Any))
1 2 nil [1])))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
; Unions need to support dots for this to work:
;
; (t/All [t0 b :..]
; (t/IFn [[t/Any :-> t/Any :filters {:then (is t0 0) :else (! t0 0)}]
; [t/Any :-> t/Any :filters {:then (is b 0) :else (! b 0)}] :.. b
; :-> (t/IFn [t/Any :-> t/Any :filters {:then (is (t/U t0 b :.. b) 0) :else (! (t/U t0 b :.. b) 0)}]
; [t/Any :* :-> t/Any])]))
cc/some-fn
(t/All [t0 t1 t2 t3 t4 t5]
(t/IFn [[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/U t0 t1) 0) :else (! (t/U t0 t1) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/U t0 t1 t2) 0) :else (! (t/U t0 t1 t2) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
[t/Any :-> t/Bool :filters {:then (is t3 0) :else (! t3 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/U t0 t1 t2 t3) 0) :else (! (t/U t0 t1 t2 t3) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
[t/Any :-> t/Bool :filters {:then (is t3 0) :else (! t3 0)}]
[t/Any :-> t/Bool :filters {:then (is t4 0) :else (! t4 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/U t0 t1 t2 t3 t4) 0) :else (! (t/U t0 t1 t2 t3 t4) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
[t/Any :-> t/Bool :filters {:then (is t3 0) :else (! t3 0)}]
[t/Any :-> t/Bool :filters {:then (is t4 0) :else (! t4 0)}]
[t/Any :-> t/Bool :filters {:then (is t5 0) :else (! t5 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/U t0 t1 t2 t3 t4 t5) 0) :else (! (t/U t0 t1 t2 t3 t4 t5) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Any] :+ :-> [t/Any :* :-> t/Any]]))
cc/every-pred
(t/All [t0 t1 t2 t3 t4 t5]
(t/IFn [[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/I t0 t1) 0) :else (! (t/I t0 t1) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/I t0 t1 t2) 0) :else (! (t/I t0 t1 t2) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
[t/Any :-> t/Bool :filters {:then (is t3 0) :else (! t3 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/I t0 t1 t2 t3) 0) :else (! (t/I t0 t1 t2 t3) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Bool :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Bool :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Bool :filters {:then (is t2 0) :else (! t2 0)}]
[t/Any :-> t/Bool :filters {:then (is t3 0) :else (! t3 0)}]
[t/Any :-> t/Bool :filters {:then (is t4 0) :else (! t4 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/I t0 t1 t2 t3 t4) 0) :else (! (t/I t0 t1 t2 t3 t4) 0)}]
[t/Any :* :-> t/Any])]
[[t/Any :-> t/Any :filters {:then (is t0 0) :else (! t0 0)}]
[t/Any :-> t/Any :filters {:then (is t1 0) :else (! t1 0)}]
[t/Any :-> t/Any :filters {:then (is t2 0) :else (! t2 0)}]
[t/Any :-> t/Any :filters {:then (is t3 0) :else (! t3 0)}]
[t/Any :-> t/Any :filters {:then (is t4 0) :else (! t4 0)}]
[t/Any :-> t/Any :filters {:then (is t5 0) :else (! t5 0)}]
:-> (t/IFn [t/Any :-> t/Bool :filters {:then (is (t/I t0 t1 t2 t3 t4 t5) 0) :else (! (t/I t0 t1 t2 t3 t4 t5) 0)}]
[t/Any :* :-> t/Any])]
[(t/+ [t/Any :-> t/Any]) :-> [t/Any :* :-> t/Any]]))
typedclojure/typedclojure
(ns typed-test.spec.clojure.core
(:require [typed.spec.clojure.core :as api]
[clojure.alpha.spec :as s]
[clojure.alpha.spec.gen :as gen]
[clojure.alpha.spec.test :as stest]
[typed.clj.spec :as t]
[typed.clj.spec.test-utils :as tu]
[clojure.test :refer :all]))
(deftest reduced-spec-test
(doseq [v (gen/sample
(s/gen (api/reduced-spec integer?)))]
(assert (reduced? v) v)
(is (integer? @v) v))
(tu/is-valid (api/reduced-spec integer?) (reduced 1))
(tu/is-invalid (api/reduced-spec symbol?) (reduced 1))
(tu/is-valid (s/or :reduced (api/reduced-spec integer?)
:integer integer?)
(reduced 1))
(tu/is-invalid (s/or :reduced (api/reduced-spec integer?)
:integer integer?)
(gensym))
(tu/is-invalid (api/reduced-spec integer?)
(gensym))
(is (apply
(every-pred
reduced?
(comp integer? deref))
(gen/sample
(s/gen (api/reduced-spec integer?)))))
)
typedclojure/typedclojure
(ns clojure.core.typed.test.pred
(:require
[clojure.test :refer :all]
[clojure.core.typed :as t]))
(deftest hmap-pred-test
(is ((every-pred
(t/pred
(t/HMap)))
{}
{:a 'blah}))
(is ((complement
(t/pred
(t/HMap :mandatory {:a Number})))
{}))
(is ((t/pred
(t/HMap :mandatory {:a Number}))
{:a 1}))
(is ((every-pred
(t/pred
(t/HMap :optional {:a Number})))
{:a 1}
{}
{:b 'a}))
(is ((every-pred
(t/pred
(t/HMap :absent-keys #{:a})))
{:b 'a}))
(is (not
((every-pred
(t/pred
(t/HMap :absent-keys #{:a})))
{:a 'a})))
)
(deftest hvec-pred-test
(is ((t/pred '[Number Number])
[1 2]))
(is ((every-pred
(complement
(t/pred '[Number Number])))
['a 2]
[]
[1]
[1 2 3]))
(is ((every-pred
(t/pred
'[Number Number Number *]))
[1 2]
[1 2 3]
[1 2 3 4 5 6 6 7 4 2 1]))
(is ((every-pred
(t/pred
'[Number Number Number :*]))
[1 2]
[1 2 3]
[1 2 3 4 5 6 6 7 4 2 1]))
(is ((every-pred
(complement
(t/pred
'[Number Number Number *])))
[]
[1]
[1 2 'a]
[1 2 3 4 5 'a 6 7 4 2 1])))
(deftest rec-pred-test
(is ((every-pred
(t/pred (t/Rec [x] (t/U '[x] Number))))
1
'[1]
'[[1]]
'[[[[[2.2]]]]]))
(is ((every-pred
(t/pred (t/Rec [x] (t/U '{:a x} Number))))
1
'{:a 1}
'{:a {:a 1}}
'{:a {:a {:a {:a {:a 1}}}}}))
(is ((every-pred
(complement
(t/pred (t/Rec [x] (t/U '[x] Number)))))
'[1 1]
'[[1] [1]])))
(deftest singleton-pred-test
(is ((t/pred true)
true))
(is ((t/pred (t/Value true))
true))
(is ((t/pred false)
false))
(is ((t/pred (t/Value false))
false))
(is ((t/pred 'sym)
'sym))
(is ((t/pred (t/Value sym))
'sym))
(is ((t/pred ':sym)
':sym))
(is ((t/pred (t/Value :sym))
':sym))
(is ((t/pred nil)
nil))
(is ((t/pred (t/Value nil))
nil))
(is ((t/pred '1)
1))
(is ((every-pred
(complement
(t/pred '1)))
1.0))
(is ((t/pred (t/Value 1))
1)))
(deftest countrange-pred-test
(is ((every-pred
(t/pred (t/CountRange 0)))
nil
[]
{}
'()))
(is ((every-pred
(complement
(t/pred (t/CountRange 0))))
; only supports clojure collections
(into-array [])
)))
(deftest intersect-pred-test
(is ((every-pred
(t/pred (t/I Number Long)))
1))
(is ((every-pred
(complement
(t/pred (t/I Number Long))))
1.1))
(is ((every-pred
(complement
(t/pred (t/I Number Long))))
1.1)))
(deftest union-pred-test
(is ((every-pred
(t/pred (t/U Number Long)))
1
1.1))
(is ((every-pred
(complement
(t/pred (t/U Number Long))))
'a))
(is ((every-pred
(complement
(t/pred t/Nothing))
(complement
(t/pred (t/U))))
'a)))
(deftest tfn-name-test
(is ((every-pred
(t/pred (clojure.core.typed/Option Number)))
nil
1
1.1)))
(deftest iseq-pred-test
(is ((every-pred
(t/pred (t/Seq Number)))
'(1 2)))
(is ((every-pred
(complement
(t/pred (t/Seq Number))))
[1 2])))
(deftest any-pred-test
(is ((every-pred
(t/pred t/Any))
1 2 nil [1])))