Skip to content

Instantly share code, notes, and snippets.

@jellea
Created September 16, 2016 10:46
Show Gist options
  • Save jellea/3aaae8074b4c156b8cdeb1bf21442c70 to your computer and use it in GitHub Desktop.
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.
(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