-
Notifications
You must be signed in to change notification settings - Fork 54
/
delta.cljc
277 lines (238 loc) · 5.5 KB
/
delta.cljc
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
(ns meander.util.delta
(:require [clojure.zip :as zip]))
(defn cljs-env?
"true if compiling ClojureScript or in a ClojureScript setting,
false otherwise."
[env]
#?(:clj (some? (:ns env))
:cljs true))
(defn parse-int
"Parse the string s as an integer."
[s]
#?(:clj (Integer/parseInt s)
:cljs (js/parseInt s)))
(defn swap
"Swap the elements at positions `i` and `j` in `v`."
{:private true}
[v i j]
(-> v
(assoc i (nth v j))
(assoc j (nth v i))))
;; SEE: https://en.wikipedia.org/wiki/Heap%27s_algorithm
(defn permutations
"Return a sequence of all the ways to arrange coll."
[coll]
(let [v (vec coll)
n (count v)]
(loop [n n
a [v]]
(if (zero? n)
a
(let [n* (dec n)
a* (mapcat
(fn step [v]
(map
(fn [i]
(swap v i n*))
(range n)))
a)]
(recur n* a*))))))
(defn k-combinations
"All the ways to choose k items from coll."
[coll k]
(if (= k 1)
(sequence (map vector) coll)
(let [coll (vec coll)
n (count coll)]
(sequence
(comp
(reduce comp
(repeat (dec k)
(mapcat
(fn [v]
(let [i (peek v)]
(map conj (repeat v) (range i)))))))
(mapcat permutations)
(map
(fn [ptrs]
(mapv nth (repeat coll) ptrs))))
(map vector (range n))))))
(defn vsplit-at
"Like `clojure.core/split-at` but for vectors."
([n v]
{:pre [(vector? v)]}
(let [i (min n (count v))]
[(subvec v 0 i) (subvec v i)])))
(defn vec-partitions [n v]
{:private true}
"
(let [coll [:a :b]
n 3]
(vec-partitions n coll))
;; => ([[] [] [:a :b]]
;; [[] [:a] [:b]]
;; [[:a] [] [:b]]
;; [[] [:a :b] []]
;; [[:a] [:b] []]
;; [[:a :b] [] []])
"
{:pre [(nat-int? n)]}
(case n
0 (list [])
1 (list [v])
2 (sequence
(map
(fn [i]
[(subvec v 0 i) (subvec v i)]))
(range (inc (count v))))
;; else
(sequence
(comp (map-indexed
(fn [i _]
[(subvec v 0 i) (subvec v i)]))
(mapcat
(fn [[a b]]
(sequence
(map conj)
(vec-partitions (dec n) a)
(repeat b)))))
(range (inc (count v))))))
(defn coll-partitions
{:private true}
([n coll]
{:pre [(nat-int? n)]}
(case n
0 (list [])
1 (list [coll])
2 (sequence
(map-indexed
(fn [i _]
(split-at i coll)))
(cons 1 coll))
;; else
(sequence
(comp
(map-indexed
(fn [i _]
(split-at i coll)))
(mapcat
(fn [[a b]]
(sequence
(map conj)
(coll-partitions (dec n) a)
(repeat b)))))
;; Adding one more element to the coll ensures we split at 0
;; *and* at (count coll) without counting the collection.
(cons (first coll) coll)))))
(defn str-partitions [n str]
"
Examples:
(let [str \"ab\"
n 0]
(str-partitions n str))
;; => ([])
(let [str \"ab\"
n 1]
(partitions n coll))
;; => ([\"ab\"])
(let [str \"ab\"
n 2]
(partitions n coll))
;; => ([[] [\"ab\"]
;; [[\"a\"] [\"b\"]]
;; [[\"ab\"] []])
(let [str \"ab\"
n 3]
(partitions n coll))
;; => ([[] [] [\"ab\"]]
;; [[] [\"a\"] [\"b\"]]
;; [[\"a\"] [] [\"b\"]]
;; [[] [\"ab\"] []]
;; [[\"a\"] [\"b\"] []]
;; [[\"ab\"] [] []])
"
{:pre [(nat-int? n)]}
(case n
0 (list [])
1 (list [str])
2 (sequence
(map
(fn [i]
[(subs str 0 i) (subs str i)]))
(range (inc (.length str))))
;; else
(sequence
(comp
(map
(fn [i]
[(subs str 0 i) (subs str i)]))
(mapcat
(fn [[a b]]
(sequence
(map conj)
(str-partitions (dec n) a)
(repeat b)))))
(range (inc (.length str))))))
(defn partitions [n coll]
"
Examples:
(def coll [:a :b])
(partitions 0 coll))
;; => ([])
(partitions 1 coll)
;; => ([[:a :b]])
(partitions 2 coll)
;; => '([[] [:a :b]]
;; [[:a] [:b]]
;; [[:a :b] []])
(partitions 3 coll)
;; => '([[] [] [:a :b]]
;; [[] [:a] [:b]]
;; [[:a] [] [:b]]
;; [[] [:a :b] []]
;; [[:a] [:b] []]
;; [[:a :b] [] []])
"
(cond
(vector? coll)
(vec-partitions n coll)
(coll? coll)
(coll-partitions n coll)
(string? coll)
(str-partitions n coll)
:else
(throw (ex-info "coll must be a string? or coll?" {:type (type coll)}))))
(defn coll-zip
"Return a zipper with a branch? fn of coll?."
[root]
(zip/zipper coll? seq
(fn [coll coll-new]
(cond
(seq? coll)
coll-new
(map? coll)
(into {} coll-new)
(map-entry? coll)
(vec coll-new)
:else
(into (empty coll) coll-new)))
root))
(defn zip-next-seq
"Given a clojure.zip zipper location loc return a lazy sequence of
all clojure.zip/next locations from loc."
[loc]
(if (zip/end? loc)
()
(lazy-seq (cons loc (zip-next-seq (zip/next loc))))))
(defn rank
"Returns a sorted sequence of values in xs by frequency of
occurence."
[xs]
(map first (sort-by (comp - val) (frequencies xs))))
(defn re-matches?
"Returns true if s matches the regex pattern re, false otherwise."
[re s]
#?(:clj
(.matches (re-matcher re s))
:cljs
(.test re s)))