Last active
May 9, 2017 13:51
-
-
Save deque-blog/f3f270ff7a9dd3c174009e40525713a3 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 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