Pre: Add util/group-by-kv.
This commit is contained in:
parent
8bb0dcfa2e
commit
bc011bbf43
2 changed files with 19 additions and 0 deletions
|
@ -154,3 +154,17 @@
|
||||||
|
|
||||||
(defn unlimited-buffer []
|
(defn unlimited-buffer []
|
||||||
(UnlimitedBuffer. #?(:cljs (array) :clj (java.util.LinkedList.))))
|
(UnlimitedBuffer. #?(:cljs (array) :clj (java.util.LinkedList.))))
|
||||||
|
|
||||||
|
(defn group-by-kv
|
||||||
|
"Returns a map of the elements of coll keyed by the first element of
|
||||||
|
the result of f on each element. The value at each key will be a
|
||||||
|
vector of the second element of the result of f on the corresponding
|
||||||
|
elements, in the order they appeared in coll."
|
||||||
|
{:static true}
|
||||||
|
[f coll]
|
||||||
|
(persistent!
|
||||||
|
(reduce
|
||||||
|
(fn [ret x]
|
||||||
|
(let [[k v] (f x)]
|
||||||
|
(assoc! ret k (conj (get ret k []) v))))
|
||||||
|
(transient {}) coll)))
|
||||||
|
|
|
@ -46,3 +46,8 @@
|
||||||
(is (util/unblocking-chan? (a/chan (a/sliding-buffer 10))))
|
(is (util/unblocking-chan? (a/chan (a/sliding-buffer 10))))
|
||||||
(is (util/unblocking-chan? (a/chan (util/unlimited-buffer))))
|
(is (util/unblocking-chan? (a/chan (util/unlimited-buffer))))
|
||||||
(is (not (util/unblocking-chan? (a/chan (a/buffer 10))))))
|
(is (not (util/unblocking-chan? (a/chan (a/buffer 10))))))
|
||||||
|
|
||||||
|
(deftest test-group-by-kvs
|
||||||
|
(are [m xs] (= m (util/group-by-kv identity xs))
|
||||||
|
{:a [1 2] :b [3]}
|
||||||
|
[[:a 1] [:a 2] [:b 3]]))
|
||||||
|
|
Loading…
Reference in a new issue