Skip to content

Instantly share code, notes, and snippets.

@dgrnbrg
Created March 7, 2012 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgrnbrg/1996228 to your computer and use it in GitHub Desktop.
Save dgrnbrg/1996228 to your computer and use it in GitHub Desktop.
Rate limit clojure fn
(defn rate-limit [interval s]
(let [cache (atom {})]
(->> s
(map #(vector % (System/currentTimeMillis)))
(filter (fn [[x t]] (< (+ (get @cache x 0) interval) t)))
(map (fn [[x t]] (swap! cache assoc x t) x)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment