-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.clj
42 lines (38 loc) · 1.33 KB
/
convert.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
(ns emlyn.grid.convert
{:no-doc true}
(:require [emlyn.grid.impl :refer [width]]))
(defn to-vec
"Convert a grid to a flat vector of all values (left to right, top to bottom)."
[grid]
(.cells grid))
(defn to-vecs
"Convert a grid to a vector of vectors, one for each row."
[grid]
(let [cells (.cells grid)
w (width grid)]
(mapv #(subvec cells % (+ % w))
(range 0 (count cells) w))))
(defn to-map
"Convert a grid to a map of xy-coordinate to value
optionally filtering out some cells (by default nils)."
[grid & {:keys [filter] :or {filter some?}}]
(reduce-kv (fn [m k v]
(if (or (nil? filter)
(filter v))
(assoc m k v)
m))
{}
grid))
(defn to-maps
"Convert a grid to a map of y (or x) coordinate to map of x (or y) coordinate to value,
optionally filtering out some cells (by default nils).
By default uses y as the first coordinate to match the format used in the constructor,
pass in `:xy-order true` to use x as the first coordinate."
[grid & {:keys [filter xy-order] :or {filter some?}}]
(reduce-kv (fn [m k v]
(if (or (nil? filter)
(filter v))
(assoc-in m (if xy-order k (reverse k)) v)
m))
{}
grid))