Created
September 16, 2016 10:46
-
-
Save jellea/3aaae8074b4c156b8cdeb1bf21442c70 to your computer and use it in GitHub Desktop.
You have a map and want to prune the oldest values (fifo) on add.
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 assoc-ordered | |
[[m v :as mv] k value] | |
[(assoc m k value) | |
(if (get m k) | |
(-> | |
(remove #{k} v) | |
vec | |
(conj k)) | |
(conj (or v []) k))]) | |
(defn prune-ordered-fifo | |
[mv num] | |
(loop [[m v :as mv] mv] | |
(if (> (count v) num) | |
(recur | |
[(dissoc m (first v)) | |
(rest v)]) | |
mv))) | |
(defn assoc-ordered-fifo | |
"You have a map and want to prune the oldest values (fifo) on add. | |
Takes a vector of a map and a vector with the keys of the map. | |
" | |
[mv k value fifo-size] | |
(-> mv | |
(assoc-ordered k value) | |
(prune-ordered-fifo fifo-size))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment