Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(defn freq-map
[coll]
(reduce
(fn [freqs val]
(update freqs val (fnil + 0) 1)) ;; Adds + 1 on each value
{} ;; Initial frequency map (empty)
coll)) ;; The values to add in the map
(defmacro freq-map-m
[coll]
`(constexpr freq-map ~coll)) ;; Call the run-time version
(walk/macroexpand-all
'(freq-map-m [1 2 1 4 1 3])) ;; Macro-expansion to simulate compilation
=> {1 3, 2 1, 4 1, 3 1} ;; Values computed at compile time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment