-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
314 lines (267 loc) · 8.12 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
(ns pp-grid.core
(:require [clojure.string :as s]
[clojure.pprint]
[potemkin :as p]
[pp-grid.ansi-escape-code :as ecodes]))
(declare
add
compute-ranges
escaped-char?
grid?
render
width
height
tf-translate
transform
update-ranges
valid-key?
valid-value?
validate-key
validate-value)
(defrecord EscapedChar [escape-code value]
Object
(toString [this] (render this)))
#_:clj-kondo/ignore
(p/def-map-type Grid [m metadata]
(get [this k default-value]
(cond
(#{:mins :maxs :dimension} k) (get metadata k default-value)
(= k :min-x) (first (:mins metadata))
(= k :max-x) (first (:maxs metadata))
(= k :min-y) (second (:mins metadata))
(= k :max-y) (second (:maxs metadata))
(= k :width) (width this)
(= k :height) (height this)
:else (if (valid-key? (:dimension metadata) k)
(get m k default-value)
default-value)))
(assoc [this k v]
(when (and (validate-key (:dimension metadata) k) (validate-value v))
(if (grid? v)
(let [ds (map - k (:mins v))]
(add this (transform v (apply tf-translate ds))))
(Grid. (assoc m k v) (update-ranges metadata k)))))
(dissoc [this k]
(when (validate-key (:dimension metadata) k)
(let [new-g (Grid. (dissoc m k) metadata)
dimension (:dimension metadata)]
(with-meta new-g (apply update-ranges {:dimension dimension} (keys new-g))))))
(keys [this] (keys m))
(meta [this] metadata)
(empty [this] (Grid. {} {:dimension (:dimension metadata)}))
(with-meta [this metadata] (Grid. m metadata))
clojure.lang.IPersistentCollection
(equiv
[this x]
(if (string? x)
(= (render this) x)
(and (or (instance? java.util.Map x) (map? x))
(= x m))))
Object
(toString [this] (render this)))
(defn empty-grid
([]
(empty-grid 2))
([dimension]
{:pre [(integer? dimension)
(pos? dimension)]}
(Grid. {} {:dimension dimension})))
(defn width [x]
(cond
(string? x) (->> x
(take-while #(not= % \newline))
count)
(grid? x) (if (empty? x)
0
(inc (- (:max-x x) (:min-x x))))
:else (width (str x))))
(defn height [x]
(cond
(string? x) (->> x
(filter #(= % \newline))
count
inc)
(grid? x) (if (empty? x) 0
(inc (- (:max-y x) (:min-y x))))
:else (height (str x))))
(defn escaped-char? [x]
(instance? EscapedChar x))
(defn grid? [x]
(instance? Grid x))
(defn escaped-char [c escape-code]
(->EscapedChar escape-code c))
(defn update-ranges
[metadata & ks]
(if (empty? ks)
metadata
(let [mins (:mins metadata nil)
maxs (:maxs metadata nil)]
(assoc metadata
:mins (apply (partial map min)
(if (nil? mins) ks (conj ks mins)))
:maxs (apply (partial map max)
(if (nil? maxs) ks (conj ks maxs)))))))
(defn add
"Constructs a grid with all given grids added together."
[& gs]
(cond
(zero? (count gs)) (empty-grid)
(= 1 (count gs)) (first gs)
:else (let [[ga gb & gcs] gs]
(apply add
(apply assoc ga (mapcat identity gb))
gcs))))
(defn subtract
"Returns the first grid minus keys in rest of the grids."
[& gs]
(cond
(zero? (count gs)) (empty-grid)
(= 1 (count gs)) (first gs)
:else (let [[ga gb & gcs] gs]
(cond
(empty? ga) (empty-grid)
(empty? gb) (apply subtract ga gcs)
:else (apply subtract
(apply dissoc ga (keys gb))
gcs)))))
(defn decorate
"Decorates a grid with given ansi-escape-codes."
[g & escape-codes]
(reduce
(fn [acc [k v]]
(let [escape-code (apply str escape-codes)
new-v (if (escaped-char? v)
(->EscapedChar (apply str escape-code (:escape-code v))
(:value v))
(->EscapedChar escape-code v))]
(assoc acc k new-v)))
(empty g)
g))
(defn ++
"Convenience wrapper for add to accept grids as args."
[& gs]
(add gs))
(defn --
"Convenience wrapper for subtract to accept grids as args."
[& gs]
(subtract gs))
(defn valid-key? [dimension k]
(and
(vector? k)
(every? integer? k)
(= (count k) dimension)))
(defn valid-value? [v]
(contains? (methods render) (type v)))
(defn validate-key [dimension k]
(when-not (valid-key? dimension k)
(throw (IllegalArgumentException. (str "Key expected to be a vector of "
dimension " integers"))))
k)
(defn validate-value [v]
(when-not (valid-value? v)
(throw (IllegalArgumentException. "Value must be a char, a Grid or a EscapedChar")))
true)
(defn round [n]
(if (integer? n)
n
(Math/round (double n))))
(defn transform
"Transforms a grid into another grid using given transformation function.
A transformation function accepts a key (coordinate vector) and returns another key.
For example transformation functions, take a look at the tf-* functions."
([g f] (transform g f (:dimension g)))
([g f dimension]
(let [transformed (reduce
(fn [acc [k v]]
(let [new-k (into [] (map round (f k)))]
(assoc acc new-k v)))
(empty-grid dimension)
g)]
(with-meta transformed (apply update-ranges
{:dimension dimension}
(keys transformed))))))
(defn tf-translate [& deltas]
(fn [k]
(map + k (take (count k) (concat deltas (repeat 0))))))
(defn tf-scale [& ns]
(fn [k]
(map * k (take (count k) (concat ns (repeat 1))))))
(defn tf-transpose []
(fn [k]
(reverse k)))
(defn tf-hflip []
(fn [k]
(let [[x y] (validate-key 2 k)]
(list (- x) y))))
(defn tf-vflip []
(fn [k]
(let [[x y] (validate-key 2 k)]
(list x (- y)))))
(defn tf-project
([target-dimension]
(tf-project target-dimension identity))
([target-dimension project-fn]
(fn [k]
(take target-dimension (concat (project-fn k) (repeat 0))))))
(defn tf-rotate [radians]
(fn [k]
(let [[x y] (validate-key 2 k)]
(list
(- (* x (Math/cos radians))
(* y (Math/sin radians)))
(+ (* x (Math/sin radians))
(* y (Math/cos radians)))))))
(defn tf-rotate-90-degrees []
(fn [k]
(let [[x y] (validate-key 2 k)]
(list (- y) x))))
(defmulti ^String render-grid :dimension)
(defmethod render-grid 1 [g]
(if (empty? g)
nil
(let [min-x (:min-x g)
max-x (:max-x g)
xs (range min-x (inc max-x))
get-rendered (fn [x] (render (get g [x] " ")))]
(apply str (map get-rendered xs)))))
(defmethod render-grid 2 [g]
(if (empty? g)
nil
(let [min-x (:min-x g)
min-y (:min-y g)
max-x (:max-x g)
max-y (:max-y g)
xs (range min-x (inc max-x))
ys (range min-y (inc max-y))
get-rendered (fn [x y] (render (get g [x y] " ")))]
(s/join
\newline
(for [y ys]
(apply str (map get-rendered xs (repeat y))))))))
(defmethod render-grid :default [g]
(if (empty? g)
nil
(render-grid (transform g (tf-project 2) 2))))
(defmulti ^String render type)
(defmethod render Grid [g]
(if (empty? g)
nil
(render-grid g)))
(defmethod render EscapedChar [x]
(str (:escape-code x)
(:value x)
ecodes/ESCAPE-CODE-RESET))
(defmethod render java.lang.Character [x]
(str x))
(defmethod render String [x] x)
;;
;; Overwrite how Grid and EscapedChar get printed: Make them print the rendered string
;;
(defmethod print-method Grid [g ^java.io.Writer w]
(.write w (render g)))
(defmethod print-method EscapedChar [x ^java.io.Writer w]
(.write w (render x)))
(defmethod clojure.pprint/simple-dispatch Grid [g]
(println (render g)))
(defmethod clojure.pprint/simple-dispatch EscapedChar [x]
(println (render x)))