Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active May 9, 2017 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/f3f270ff7a9dd3c174009e40525713a3 to your computer and use it in GitHub Desktop.
Save deque-blog/f3f270ff7a9dd3c174009e40525713a3 to your computer and use it in GitHub Desktop.
(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