Skip to content

Commit 99f4463

Browse files
brandonbloomdnolen
authored andcommitted
CLJS-1243: Add TaggedLiteral type & related fns
1 parent 795f08e commit 99f4463

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/cljs/cljs/core.cljs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8956,6 +8956,7 @@ reduces them without incurring seq initialization"
89568956
[proc coll]
89578957
(reduce #(proc %2) nil coll))
89588958

8959+
89598960
(defprotocol IEncodeJS
89608961
(-clj->js [x] "Recursively transforms clj values to JavaScript")
89618962
(-key->js [x] "Transforms map keys to valid JavaScript keys. Arbitrary keys are
@@ -9610,3 +9611,40 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."
96109611
(if f
96119612
(do (f) :ok)
96129613
:no-test)))
9614+
9615+
9616+
(deftype TaggedLiteral [tag form]
9617+
9618+
IEquiv
9619+
(-equiv [this other]
9620+
(and (instance? TaggedLiteral other)
9621+
(= tag (.-tag other))
9622+
(= form (.-form other))))
9623+
9624+
IHash
9625+
(-hash [this]
9626+
(+ (* 31 (hash tag))
9627+
(hash form)))
9628+
9629+
ILookup
9630+
(-lookup [this v]
9631+
(-lookup this v nil))
9632+
(-lookup [this v not-found]
9633+
(case v
9634+
:tag tag
9635+
:form form
9636+
not-found))
9637+
9638+
)
9639+
9640+
(defn tagged-literal?
9641+
"Return true if the value is the data representation of a tagged literal"
9642+
[value]
9643+
(instance? cljs.core.TaggedLiteral value))
9644+
9645+
(defn tagged-literal
9646+
"Construct a data representation of a tagged literal from a
9647+
tag symbol and a form."
9648+
[tag form]
9649+
{:pre (symbol? tag)}
9650+
(cljs.core.TaggedLiteral. tag form))

test/cljs/cljs/core_test.cljs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,6 +2793,17 @@
27932793
(is (= (-> #'map meta :arglists)
27942794
'([f] [f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls]))))
27952795

2796+
(deftest tagged-literals
2797+
(let [tl (tagged-literal 'x "y")]
2798+
(is (tagged-literal? tl))
2799+
(is (not (tagged-literal? {:tag 'x :form "y"})))
2800+
(is (= (:tag tl) 'x))
2801+
(is (= (:form tl) "y"))
2802+
(is (= tl (tagged-literal 'x "y")))
2803+
(is (not= tl (tagged-literal 'z "y")))
2804+
(is (not= tl (tagged-literal 'x "z")))
2805+
(is (= (hash tl) (hash (tagged-literal 'x "y"))))))
2806+
27962807
(comment
27972808
;; ObjMap
27982809
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)