Skip to content

Commit cb6d9aa

Browse files
committed
miu perf
1 parent 180b717 commit cb6d9aa

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/malli/impl/util.cljc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
(defn -reduce-kv-valid [f init coll] (reduce-kv (comp #(-map-invalid reduced %) f) init coll))
1414

1515
(defn -last [x] (if (vector? x) (peek x) (last x)))
16-
(defn -some [pred coll] (reduce (fn [ret x] (if (pred x) (reduced true) ret)) nil coll))
1716
(defn -merge [m1 m2] (if m1 (persistent! (reduce-kv assoc! (transient m1) m2)) m2))
1817

1918
(defn -error
2019
([path in schema value] {:path path, :in in, :schema schema, :value value})
2120
([path in schema value type] {:path path, :in in, :schema schema, :value value, :type type}))
2221

2322
(defn -vmap
24-
([os] (-vmap identity os))
23+
([os] (if (vector? os) os (-vmap identity os)))
2524
([f os] #?(:clj (let [c (count os)]
2625
(if-not (zero? c)
2726
(let [oa (object-array c), iter (.iterator ^Iterable os)]
@@ -41,33 +40,43 @@
4140
#?(:clj
4241
(defmacro -combine-n
4342
[c n xs]
43+
(assert (simple-symbol? xs))
4444
(let [syms (repeatedly n gensym)
45-
g (gensym "preds__")
46-
bs (interleave syms (map (fn [n] `(nth ~g ~n)) (range n)))
45+
bs (interleave syms (map (fn [n] `(nth ~xs ~n)) (range n)))
4746
arg (gensym "arg__")
4847
body `(~c ~@(map (fn [sym] `(~sym ~arg)) syms))]
49-
`(let [~g (-vmap ~xs) ~@bs]
48+
`(let [~@bs]
5049
(fn [~arg] ~body)))))
5150

51+
#?(:clj (defmacro OR
52+
([] false)
53+
([x] `(if ~x true false))
54+
([x & next] `(if ~x true (OR ~@next)))))
55+
56+
#?(:clj (defmacro AND
57+
([] true)
58+
([x] `(if ~x true false))
59+
([x & next] `(if ~x (AND ~@next) false))))
60+
5261
#?(:clj
5362
(defmacro -pred-composer
5463
[c n]
5564
(let [preds (gensym "preds__")
5665
f (gensym "f__")
5766
cases (mapcat (fn [i] [i `(-combine-n ~c ~i ~preds)]) (range 2 (inc n)))
58-
else `(let [p# (~f (take ~n ~preds)) q# (~f (drop ~n ~preds))]
67+
else `(let [p# (~f (subvec ~preds 0 ~n)) q# (~f (subvec ~preds ~n))]
5968
(fn [x#] (~c (p# x#) (q# x#))))]
6069
`(fn ~f [~preds]
6170
(case (count ~preds)
62-
0 (constantly (boolean (~c)))
63-
1 (first ~preds)
71+
0 (constantly (~c))
72+
1 (nth ~preds 0)
6473
~@cases
6574
~else)))))
6675

6776
(def ^{:arglists '([[& preds]])} -every-pred
68-
#?(:clj (-pred-composer and 16)
69-
:cljs (fn [preds] (fn [m] (boolean (reduce #(or (%2 m) (reduced false)) true preds))))))
77+
#?(:clj (-pred-composer AND 16)
78+
:cljs (fn [preds] (fn [m] (reduce #(if (%2 m) true (reduced false)) true preds)))))
7079

7180
(def ^{:arglists '([[& preds]])} -some-pred
72-
#?(:clj (-pred-composer or 16)
73-
:cljs (fn [preds] (fn [x] (boolean (some #(% x) preds))))))
81+
#?(:clj (-pred-composer OR 16)
82+
:cljs (fn [preds] (fn [m] (reduce #(if (%2 m) (reduced true) false) false preds)))))

0 commit comments

Comments
 (0)