Skip to content

Instantly share code, notes, and snippets.

@ohassidi
ohassidi / foldmaps.clj
Created October 21, 2020 06:57 — forked from bsless/foldmaps.clj
Annotated version for Oded
(defn fold-by ;;; similar to group-by
([kf ;;; Key function to group on
op ;;; operation how to combine two elements under the same key
coll] ;; collection of maps
(persistent!
(reduce
(fn [ret x] ;; ret is a map from (kf x) to xs, similar to the result of group-by
(let [k (kf x) ;; derive the key
v (get ret k)] ;; get the value already associated with the key if it exists
(assoc! ret