-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocols.clj
210 lines (170 loc) · 7.03 KB
/
protocols.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(ns cljs-compat.protocols
"Map from Clojure to ClojureScript protocols")
(declare protocol-map protocol-method-map)
(defn transmogrify
"Based on PROTOCOL and METHOD returns map of:
:protocol target protocol
:method target method
:append additional compatibility code to append to extend-type or
deftype / defrecord"
([PROTOCOL METHOD]
(transmogrify protocol-map protocol-method-map PROTOCOL METHOD))
([PROTO-MAP METHOD-MAP PROTOCOL METHOD]
(let [proto (or (PROTO-MAP PROTOCOL) PROTOCOL)
method (or (get-in METHOD-MAP [PROTOCOL METHOD])
(when (PROTO-MAP PROTOCOL) METHOD))]
(when method
(merge {:method method :protocol proto}
(cond
(map? method) method
(symbol? method) {:method method}
(coll? method) (zipmap [:protocol :method] method)))))))
;;; definitions
(defn- clojure-lang
[SEQ]
(->> SEQ
(map #(let [[clj cljs] (if (coll? %) % [% %])]
(vector (-> (str "clojure.lang." clj) symbol)
(symbol "cljs.core" (str cljs))
)))
(into {})))
(def protocol-map
(merge (clojure-lang '[IFn
[Counted ICounted]
; IEmptyableCollection
[IPersistentCollection ICollection]
[Indexed IIndexed]
ISeq
; INext
ILookup
[Associative IAssociative]
[IPersistentMap IMap]
IMapEntry
[IPersistentSet ISet]
[IPersistentStack IStack]
[IPersistentVector IVector]
IDeref
IDerefWithTimeout
IMeta
;; IWithMeta
IReduce
IKVReduce
;; IEquiv
IHash
[Seqable ISeqable]
;; ISequential
[Reversible IReversible]
[Sorted ISorted]
;; I do not expect to have cross-plafrom IO
;; IWriter
IPending
;; Neither IRef (watches, validators, ...)
;; [IRef IWatchable]
IEditableCollection
ITransientCollection
ITransientAsociative
ITransientMap
ITransientVector
ITransientSet
IComparable]) ; todo
'{Object Object,
java.lang.Object Object
clojure.core.protocols.CollReduce IReduce
clojure.core.protocols.InternalReduce IReduce
clojure.core.protocols.IKVReduce IKVReduce}))
(def protocol-method-map
'{java.lang.Object
{equals [IEquiv -equiv]
hashCode [IHash -hash]
compareTo [IComparable -compare]}
Object
{equals [IEquiv -equiv]
hashCode [IHash -hash]
compareTo [IComparable -compare]}
clojure.lang.Counted
(count -icount)
clojure.lang.IFn
(invoke -invoke)
clojure.lang.ILookup
{valAt -lookup}
clojure.lang.ISeq
{first -first
next [INext -next]
more -rest
cons {:method nil}}
clojure.lang.IPersistentCollection
{count [ICounted -count]
cons -conj
empty [IEmptyableCollection -empty]
equiv {:method nil}}
clojure.lang.Seqable
{seq -seq}
clojure.lang.Indexed
{nth -nth}
clojure.lang.Associative
{containsKey -contains-key?
entryAt {:method nil}
assoc -assoc}
clojure.lang.IPersistentMap
{assoc -assoc
assocEx {:method nil}
without -dissoc}
clojure.lang.IMapEntry
{key -key
val -val}
clojure.lang.IPersistentSet
{disjoin -disjoin
contains [IAssociative -contains-key?]
get [ILookup -lookup]}
clojure.lang.IPersistentStack
{peek -peek
pop -pop}
clojure.lang.IPersistentVector
{length [ICounted -count]
assocN -assoc-n
cons [ICollection -conj]}
clojure.lang.IDeref
{deref -deref}
clojure.lang.IBlockingDeref
{deref -deref-with-timeout}
clojure.lang.IMeta
{meta -meta}
clojure.lang.IObj
{withMeta [IWithMeta -with-meta]}
clojure.lang.IReduce
{reduce -reduce}
clojure.core.protocols.IKVReduce
{kv-reduce -kv-reduce}
clojure.core.protocols.CollReduce
{coll-reduce -reduce}
clojure.core.protocols.InternalReduce
{internal-reduce -reduce}
clojure.lang.Reversible
{rseq -rseq}
clojure.lang.Sorted
{comparator -comparator
entryKey -entry-key
seqFrom {:method -sorted-seq-from
:append [[cljs.core/ISorted
(-sorted-seq [S ASCENDING?]
((if ASCENDING? -seq -rseq) S))]]}}
clojure.lang.IPending
{isRealized -realized?}
clojure.lang.IEditableCollection
{asTransient -as-transient}
clojure.lang.ITransientCollection
{conj -conj!
persistent -persistent!}
clojure.lang.ITransientAssociative
{assoc -assoc!}
clojure.lang.ITransientMap
{assoc [ITransientAssociative -assoc!]
without -dissoc!
persistent [ITransientCollection -persistent!]}
clojure.lang.ITransientVector
{assocN -assoc-n!
pop -pop!}
clojure.lang.ITransientSet
{disjoin -disjoin!
contains [IAssociative -contains-key?]
get {:method nil}}})