Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(defn group-by-delimiter-and-map* [pred keymap valmap [groups current-delim current-group] elem]
(if (pred elem)
[(if current-delim
(assoc groups current-delim current-group)
(keymap elem) []]
[groups current-delim (conj current-group (valmap elem))]))
(defn group-by-delimiter-and-map [pred keymap valmap coll]
(let [[groups last-delim last-group]
(reduce (partial group-by-delimiter-and-map* pred keymap valmap) [{}] coll)]
(assoc groups last-delim last-group)))
; user> (group-by-delimiter-and-map number? #(* % %) str '(1 a b 5 c d e 7 x y z))
{49 ["x" "y" "z"], 25 ["c" "d" "e"], 1 ["a" "b"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.