|
13 | 13 | (defn -reduce-kv-valid [f init coll] (reduce-kv (comp #(-map-invalid reduced %) f) init coll)) |
14 | 14 |
|
15 | 15 | (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)) |
17 | 16 | (defn -merge [m1 m2] (if m1 (persistent! (reduce-kv assoc! (transient m1) m2)) m2)) |
18 | 17 |
|
19 | 18 | (defn -error |
20 | 19 | ([path in schema value] {:path path, :in in, :schema schema, :value value}) |
21 | 20 | ([path in schema value type] {:path path, :in in, :schema schema, :value value, :type type})) |
22 | 21 |
|
23 | 22 | (defn -vmap |
24 | | - ([os] (-vmap identity os)) |
| 23 | + ([os] (if (vector? os) os (-vmap identity os))) |
25 | 24 | ([f os] #?(:clj (let [c (count os)] |
26 | 25 | (if-not (zero? c) |
27 | 26 | (let [oa (object-array c), iter (.iterator ^Iterable os)] |
|
41 | 40 | #?(:clj |
42 | 41 | (defmacro -combine-n |
43 | 42 | [c n xs] |
| 43 | + (assert (simple-symbol? xs)) |
44 | 44 | (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))) |
47 | 46 | arg (gensym "arg__") |
48 | 47 | body `(~c ~@(map (fn [sym] `(~sym ~arg)) syms))] |
49 | | - `(let [~g (-vmap ~xs) ~@bs] |
| 48 | + `(let [~@bs] |
50 | 49 | (fn [~arg] ~body))))) |
51 | 50 |
|
| 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 | + |
52 | 61 | #?(:clj |
53 | 62 | (defmacro -pred-composer |
54 | 63 | [c n] |
55 | 64 | (let [preds (gensym "preds__") |
56 | 65 | f (gensym "f__") |
57 | 66 | 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))] |
59 | 68 | (fn [x#] (~c (p# x#) (q# x#))))] |
60 | 69 | `(fn ~f [~preds] |
61 | 70 | (case (count ~preds) |
62 | | - 0 (constantly (boolean (~c))) |
63 | | - 1 (first ~preds) |
| 71 | + 0 (constantly (~c)) |
| 72 | + 1 (nth ~preds 0) |
64 | 73 | ~@cases |
65 | 74 | ~else))))) |
66 | 75 |
|
67 | 76 | (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))))) |
70 | 79 |
|
71 | 80 | (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