-
Notifications
You must be signed in to change notification settings - Fork 2
/
coll.cljc
135 lines (115 loc) · 3.28 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
(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 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 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))))