-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.cljc
46 lines (42 loc) · 1.36 KB
/
core.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
(ns mate.core
#?(:cljs (:require-macros mate.core))
(:refer-clojure :exclude [group-by])
(:require [clojure.core :as cc]))
(defn seq-indexed
"Returns an indexed sequence from the collection `coll`."
[coll]
(map-indexed vector coll))
(defmacro comp->
"Same as `comp` but with the arguments in reverse order."
[& args]
`(comp ~@(reverse args)))
(defn group-by
"Same as clojure.core/group-by, but with some handy new arities which apply
custom map & reduce operations to the elements grouped together under the same key."
([kf coll]
;(group-by kf identity conj [] coll)
(cc/group-by kf coll))
([kf vf coll]
(group-by kf vf conj [] coll))
([kf vf rf coll]
(group-by kf vf rf (rf) coll))
([kf vf rf init coll]
(->> coll
(reduce (fn [ret x]
(let [k (kf x)
v (vf x)]
(assoc! ret k (rf (get ret k init) v))))
(transient {}))
persistent!)))
(defn index-by
"Returns a hashmap made of `[key value]` pairs from items in the collection `coll`
where the keys are `(kf item)` and the values are `(vf item)`.
`vf` defaults to the identify function."
([kf coll]
(index-by kf identity coll))
([kf vf coll]
(->> coll
(reduce (fn [ret x]
(assoc! ret (kf x) (vf x)))
(transient {}))
persistent!)))