Skip to content

Commit 733ff82

Browse files
author
dnolen
committed
same as Clojure f571c4bb
1 parent 0e608c9 commit 733ff82

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/main/cljs/cljs/spec.cljc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@
153153
154154
(s/or :even even? :small #(< % 42))
155155
156-
Returns a destructuring spec that
157-
returns a vector containing the key of the first matching pred and the
158-
corresponding value."
156+
Returns a destructuring spec that returns a map entry containing the
157+
key of the first matching pred and the corresponding value. Thus the
158+
'key' and 'val' functions can be used to refer generically to the
159+
components of the tagged return."
159160
[& key-pred-forms]
160161
(let [pairs (partition 2 key-pred-forms)
161162
keys (mapv first pairs)
@@ -197,8 +198,10 @@
197198
198199
(s/alt :even even? :small #(< % 42))
199200
200-
Returns a regex op that returns a vector containing the key of the
201-
first matching pred and the corresponding value."
201+
Returns a regex op that returns a map entry containing the key of the
202+
first matching pred and the corresponding value. Thus the
203+
'key' and 'val' functions can be used to refer generically to the
204+
components of the tagged return."
202205
[& key-pred-forms]
203206
(let [pairs (partition 2 key-pred-forms)
204207
keys (mapv first pairs)

src/main/cljs/cljs/spec.cljs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@
603603
(with-gen* [_ gfn] (tuple-impl forms preds gfn))
604604
(describe* [_] `(tuple ~@forms)))))
605605

606+
(defn- tagged-ret [v]
607+
(specify! v
608+
IMapEntry
609+
(-key [_] (-nth v 0))
610+
(-val [_] (-nth v 1))))
606611

607612
(defn ^:skip-wiki or-spec-impl
608613
"Do not call this directly, use 'or'"
@@ -616,7 +621,7 @@
616621
(let [ret (dt pred x (nth forms i))]
617622
(if (= ::invalid ret)
618623
(recur (inc i))
619-
[(keys i) ret])))
624+
(tagged-ret [(keys i) ret]))))
620625
::invalid)))]
621626
(reify
622627
IFn
@@ -747,7 +752,7 @@
747752
(if (nil? pr)
748753
(if k1
749754
(if (accept? p1)
750-
(accept [k1 (:ret p1)])
755+
(accept (tagged-ret [k1 (:ret p1)]))
751756
ret)
752757
p1)
753758
ret)))))
@@ -799,7 +804,7 @@
799804
::pcat (add-ret p0 ret k)
800805
::alt (let [[[p0] [k0]] (filter-alt ps ks forms accept-nil?)
801806
r (if (nil? p0) ::nil (preturn p0))]
802-
(if k0 [k0 r] r)))))
807+
(if k0 (tagged-ret [k0 r]) r)))))
803808

804809
(defn- op-unform [p x]
805810
;;(prn {:p p :x x})

0 commit comments

Comments
 (0)