Skip to content

Commit

Permalink
Add group-by
Browse files Browse the repository at this point in the history
  • Loading branch information
madstap committed May 5, 2024
1 parent 52683d7 commit b31a1bf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/jank/clojure/core.jank
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,18 @@
(assoc counts x (inc (get counts x 0))))
{} coll))

(defn group-by
"Returns a map of the elements of coll keyed by the result of
f on each element. The value at each key will be a vector of the
corresponding elements, in the order they appeared in coll."
[f coll]
;; OPTIMIZE: transient
(reduce
(fn [ret x]
(let [k (f x)]
(assoc ret k (conj (get ret k []) x))))
{} coll))

(defn reductions
"Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init."
Expand Down

0 comments on commit b31a1bf

Please sign in to comment.