Created
August 6, 2010 21:36
-
-
Save jkk/512047 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ;; inspired by http://github.com/Seajure/reducate | |
| (defmacro accumulate | |
| [[acc-sym acc-expr & [bind-sym bind-expr & bind-more]] body] | |
| `(reduce | |
| (fn ~[acc-sym bind-sym] | |
| ~(if (empty? bind-more) | |
| body | |
| `(accumulate | |
| ~(into [acc-sym acc-sym] bind-more) | |
| ~body))) | |
| ~acc-expr | |
| ~bind-expr)) | |
| (def movies->stars | |
| {"Lawrence of Arabia" ["Peter O'Toole" | |
| "Omar Sharif" | |
| "Claude Rains"] | |
| "Casablanca" ["Humphrey Bogart" | |
| "Claude Rains" | |
| "Peter Lorre"] | |
| "M" ["Peter Lorre"]}) | |
| ;; nested reduce -- awkward | |
| (def stars->movies | |
| (reduce | |
| (fn [m [movie stars]] | |
| (reduce #(assoc %1 %2 (conj (%1 %2 []) movie)) | |
| m | |
| stars)) | |
| {} | |
| movies->stars)) | |
| ;; more readable, still transient-capable | |
| (def stars->movies | |
| (accumulate [m {} | |
| [movie stars] movies->stars | |
| star stars] | |
| (assoc m star (conj (m star []) movie)))) | |
| ;; another alternative, but slower | |
| (def stars->movies | |
| (apply merge-with into | |
| (for [[movie stars] movies->stars | |
| star stars] | |
| {star [movie]}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment