-
-
Save tomjack/a625a6edd5824bfc4c81 to your computer and use it in GitHub Desktop.
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 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) | |
groups) | |
(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