This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |