-
Notifications
You must be signed in to change notification settings - Fork 2
/
coll.cljc
208 lines (181 loc) · 4.64 KB
/
coll.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
(ns com.wsscode.misc.coll
(:require
[clojure.set :as set])
#?(:clj
(:import
(clojure.lang
MapEntry))))
(defn distinct-by
"Returns a lazy sequence of the elements of coll, removing any elements that
return duplicate values when passed to a function f."
([f]
(fn [rf]
(let [seen (volatile! #{})]
(fn
([] (rf))
([result] (rf result))
([result x]
(let [fx (f x)]
(if (contains? @seen fx)
result
(do (vswap! seen conj fx)
(rf result x)))))))))
([f coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[x :as xs] seen]
(when-let [s (seq xs)]
(let [fx (f x)]
(if (contains? seen fx)
(recur (rest s) seen)
(cons x (step (rest s) (conj seen fx)))))))
xs seen)))]
(step coll #{}))))
(defn dedupe-by
"Returns a lazy sequence removing consecutive duplicates in coll when passed to a function f.
Returns a transducer when no collection is provided."
{:added "1.7"}
([f]
(fn [rf]
(let [pv (volatile! ::none)]
(fn
([] (rf))
([result] (rf result))
([result x]
(let [prior @pv
fx (f x)]
(vreset! pv fx)
(if (= prior fx)
result
(rf result x))))))))
([f coll] (sequence (dedupe-by f) coll)))
(defn index-by
"Like group by, but will keep only the last result."
[f coll]
(reduce
(fn [m x]
(assoc m (f x) x))
{}
coll))
(def sconj (fnil conj #{}))
(def vconj (fnil conj []))
(defn queue
"Return a blank immutable queue or create one from coll."
([] #?(:clj clojure.lang.PersistentQueue/EMPTY
:cljs cljs.core/PersistentQueue.EMPTY))
([coll]
(into (queue) coll)))
(defn make-map-entry
"CLJC helper to create MapEntry."
[k v]
#?(:clj
(MapEntry. k v)
:cljs
(MapEntry. k v nil)))
(defn map-keys
"Map over the given hash-map keys.
Example:
(map-keys #(str/replace (name %) \"_\" \"-\") {\"foo_bar\" 1}) => {\"foo-bar\" 1}
"
[f m]
(into {} (map (fn [x] (make-map-entry (f (key x)) (val x)))) m))
(defn map-vals
"Map over the given hash-map vals.
Example:
(map-vals inc {:a 1 :b 2})
"
[f m]
(into {} (map (fn [x] (make-map-entry (key x) (f (val x))))) m))
(defn filter-keys
[f m]
(into {} (filter (comp f key)) m))
(defn filter-vals
[f m]
(into {} (filter (comp f val)) m))
(defn remove-keys
[f m]
(into {} (remove (comp f key)) m))
(defn remove-vals
[f m]
(into {} (remove (comp f val)) m))
(defn keys-set
"Return the map keys, as a set. This also checks if the entry is a map, otherwise
returns nil (instead of throw)."
[m]
(if (map? m) (into #{} (keys m))))
(defn merge-grow
"Additive merging.
When merging maps, it does a deep merge.
When merging sets, makes a union of them.
When value of the right side is nil, the left side will be kept.
For the rest works as standard merge."
([] {})
([a] a)
([a b]
(cond
(and (set? a) (set? b))
(set/union a b)
(and (map? a) (map? b))
(merge-with merge-grow a b)
(nil? b) a
:else
b)))
(defn merge-defaults
"Like merge, but only add keys that are not present in the original map."
[m defaults]
(reduce-kv
(fn [m k v]
(if (contains? m k)
m
(assoc m k v)))
m
defaults))
(defn update-contained
"Update some key when that key is present in the map."
([m k f]
(if (contains? m k)
(update m k f)
m))
([m k f a1]
(if (contains? m k)
(update m k f a1)
m))
([m k f a1 a2]
(if (contains? m k)
(update m k f a1 a2)
m))
([m k f a1 a2 a3]
(if (contains? m k)
(update m k f a1 a2 a3)
m))
([m k f a1 a2 a3 & args]
(if (contains? m k)
(apply update m k f a1 a2 a3 args)
m)))
(defn update-if
"Update some key that key is present in the map and value is truthy."
([m k f]
(if (get m k)
(update m k f)
m))
([m k f a1]
(if (get m k)
(update m k f a1)
m))
([m k f a1 a2]
(if (get m k)
(update m k f a1 a2)
m))
([m k f a1 a2 a3]
(if (get m k)
(update m k f a1 a2 a3)
m))
([m k f a1 a2 a3 & args]
(if (get m k)
(apply update m k f a1 a2 a3 args)
m)))
(defn native-map? [x]
#?(:clj (or (instance? clojure.lang.PersistentArrayMap x)
(instance? clojure.lang.PersistentHashMap x))
:cljs (or (instance? cljs.core/PersistentArrayMap x)
(instance? cljs.core/PersistentHashMap x))))