-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
349 lines (296 loc) · 8.86 KB
/
core.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
(ns lazy-map.core
"Main namespace, contains utility functions, type definitions, and
lazy map functions.
Public API is `->LazyMap`, `->?LazyMap`, `lazy-map`, `force-map`,
`freeze-map`, `lazy-map-dispatch`."
(:require [clojure
[pprint :as pp]
[string :as str]])
(:import java.io.Writer))
;;;; Utility functions
(defmacro is-not-thrown?
"Used in clojure.test assertions because (is (not (thrown? ...)))
doesn't work. See http://acidwords.com/posts/2015-07-23-fixing-negation-in-clojure-test.html"
[e expr]
{:style/indent 1}
`(is (not ('thrown? ~e ~expr))))
(defmacro extend-print
"Convenience macro for overriding the string representation of a
class. Note that you also need to override `toString` in order to
customize the return value of `str` on your object, and you need to
create your own dispatch function to customize the pretty-printed
representation."
{:private true}
[class str-fn]
`(do
;; for serialization
(defmethod print-dup ~class
[obj# ^Writer writer#]
(.write writer# ^String (~str-fn obj#)))
;; for reader-friendly printing
(defmethod print-method ~class
[obj# ^Writer writer#]
(.write writer# ^String (~str-fn obj#)))))
(defn map-entry
"Creates a map entry (as returned by calling seq on a map) with the
given key and value."
[k v]
(clojure.lang.MapEntry/create k v))
(defn map-keys
"Applies f to each of the keys of a map, returning a new map."
[f map]
(reduce-kv (fn [m k v]
(assoc m (f k) v))
{}
map))
(defn map-vals
"Applies f to each of the values of a map, returning a new map."
[f map]
(reduce-kv (fn [m k v]
(assoc m k (f v)))
{}
map))
;;;; PlaceholderText type
(defrecord PlaceholderText [text])
(extend-print PlaceholderText :text)
;;;; LazyMapEntry type
(deftype LazyMapEntry [key_ val_]
clojure.lang.Associative
(containsKey [this k]
(boolean (#{0 1} k)))
(entryAt [this k]
(cond
(= k 0) (map-entry 0 key_)
(= k 1) (LazyMapEntry. 1 val_)
:else nil))
(assoc [this k v]
(cond
(= k 0) (LazyMapEntry. v val_)
(= k 1) (LazyMapEntry. key_ v)
(= k 2) (vector k (force val_) v)
:else (throw (IndexOutOfBoundsException.))))
clojure.lang.IFn
(invoke [this k]
(.valAt this k))
clojure.lang.IHashEq
(hasheq [this]
(.hasheq
^clojure.lang.IHashEq
(vector key_ (force val_))))
clojure.lang.ILookup
(valAt [this k]
(cond
(= k 0) key_
(= k 1) (force val_)
:else nil))
(valAt [this k not-found]
(cond
(= k 0) key_
(= k 1) (force val_)
:else not-found))
clojure.lang.IMapEntry
(key [this] key_)
(val [this] (force val_))
clojure.lang.Indexed
(nth [this i]
(cond
(= i 0) key_
(= i 1) (force val_)
(integer? i) (throw (IndexOutOfBoundsException.))
:else (throw (IllegalArgumentException. "Key must be integer")))
(.valAt this i))
(nth [this i not-found]
(try
(.nth this i)
(catch Exception _ not-found)))
clojure.lang.IPersistentCollection
(count [this] 2)
(empty [this] false)
(equiv [this o]
(.equiv
[key_ (force val_)]
o))
clojure.lang.IPersistentStack
(peek [this] (force val_))
(pop [this] [key_])
clojure.lang.IPersistentVector
(assocN [this i v]
(.assocN [key_ (force val_)] i v))
(cons [this o]
(.cons [key_ (force val_)] o))
clojure.lang.Reversible
(rseq [this] (lazy-seq (list (force val_) key_)))
clojure.lang.Seqable
(seq [this]
(cons key_ (lazy-seq (list (force val_)))))
clojure.lang.Sequential
java.io.Serializable
java.lang.Comparable
(compareTo [this o]
(.compareTo
^java.lang.Comparable
(vector key_ (force val_))
o))
java.lang.Iterable
(iterator [this]
(.iterator
^java.lang.Iterable
(.seq this)))
java.lang.Object
(toString [this]
(str [key_ (if (and (delay? val_)
(not (realized? val_)))
(->PlaceholderText "<unrealized>")
(force val_))]))
java.util.Map$Entry
(getKey [this] key_)
(getValue [this] (force val_))
java.util.RandomAccess)
(defn lazy-map-entry
"Construct a lazy map entry with the given key and value. If you
want to take advantage of the laziness, the value should be a
delay."
[k v]
(LazyMapEntry. k v))
(extend-print LazyMapEntry #(.toString ^LazyMapEntry %))
;;;; LazyMap type
;; We need to use this function in .toString so it needs to be defined
;; before the deftype. But the definition of this function needs a
;; ^LazyMap type hint, so the definition can't come until after the
;; deftype.
(declare freeze-map)
(deftype LazyMap [^clojure.lang.IPersistentMap contents]
clojure.lang.Associative
(containsKey [this k]
(and contents
(.containsKey contents k)))
(entryAt [this k]
(and contents
(lazy-map-entry k (.valAt contents k))))
clojure.lang.IFn
(invoke [this k]
(.valAt this k))
(invoke [this k not-found]
(.valAt this k not-found))
clojure.lang.IKVReduce
(kvreduce [this f init]
(reduce-kv f init (into {} this)))
clojure.lang.ILookup
(valAt [this k]
(and contents
(force (.valAt contents k))))
(valAt [this k not-found]
;; This will not behave properly if not-found is a Delay,
;; but that's a pretty obscure edge case.
(and contents
(force (.valAt contents k not-found))))
clojure.lang.IMapIterable
(keyIterator [this]
(.iterator
^java.lang.Iterable
(keys contents)))
(valIterator [this]
(.iterator
;; Using the higher-arity form of map prevents chunking.
^java.lang.Iterable
(map (fn [[k v] _]
(force v))
contents
(repeat nil))))
clojure.lang.IPersistentCollection
(count [this]
(if contents
(.count contents)
0))
(empty [this]
(or (not contents)
(.empty contents)))
(cons [this o]
(LazyMap. (.cons (or contents {}) o)))
(equiv [this o]
(.equiv
^clojure.lang.IPersistentCollection
(into {} this) o))
clojure.lang.IPersistentMap
(assoc [this key val]
(LazyMap. (.assoc (or contents {}) key val)))
(without [this key]
(LazyMap. (.without (or contents {}) key)))
clojure.lang.Seqable
(seq [this]
;; Using the higher-arity form of map prevents chunking.
(map (fn [[k v] _]
(lazy-map-entry k v))
contents
(repeat nil)))
java.lang.Iterable
(iterator [this]
(.iterator
^java.lang.Iterable
(.seq this)))
java.lang.Object
(toString [this]
(str (freeze-map (->PlaceholderText "<unrealized>") this))))
(alter-meta!
#'->LazyMap assoc :doc
"Turn a regular map into a lazy map. Any values that are delays
are interpreted as values that have yet to be realized.")
(extend-print LazyMap #(.toString ^LazyMap %))
;;;; Functions for working with lazy maps
(defn ->?LazyMap
"Behaves the same as ->LazyMap, except that if m is already a lazy
map, returns it directly. This prevents the creation of a lazy map
wrapping another lazy map, which (while not terribly wrong) is not
the best idea."
[map]
(if (instance? LazyMap map)
map
(->LazyMap map)))
(defmacro lazy-map
"Constructs a lazy map from a literal map. None of the values are
evaluated until they are accessed from the map."
[map]
`(->LazyMap
~(->> map
(apply concat)
(partition 2)
(clojure.core/map (fn [[k v]] [k `(delay ~v)]))
(into {}))))
(defn force-map
"Realizes all the values in a lazy map, returning a regular map."
[map]
(into {} map))
(defn freeze-map
"Replace all the unrealized values in a lazy map with placeholders,
returning a regular map. No matter what is done to the returned map,
the values in the original map will not be forced. v can be an
object to use for all the values or a function of the key."
[val map]
(let [val (if (fn? val)
val
(constantly val))]
(reduce-kv (fn [m k v]
(assoc m k (if (and (delay? v)
(not (realized? v)))
(val k)
(force v))))
{}
(.contents ^LazyMap map))))
(defn lazy-map-dispatch
"This is a dispatch function for clojure.pprint that prints
lazy maps without forcing them."
[obj]
(cond
(instance? LazyMap obj)
(pp/simple-dispatch (freeze-map (->PlaceholderText "<unrealized>") obj))
(instance? LazyMapEntry obj)
(pp/simple-dispatch
(let [raw-value (.val_ obj)]
(assoc obj 1 (if (and (delay? raw-value)
(not (realized? raw-value)))
(->PlaceholderText "<unrealized>")
(force raw-value)))))
(instance? PlaceholderText obj)
(pr obj)
:else
(pp/simple-dispatch obj)))