Created
January 22, 2018 13:36
-
-
Save athos/e3d84503c15cc4080ef8a25d180d9664 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
(require '[clojure.core.async :as a] | |
'[kitchen-async.promise :as p] | |
'[kitchen-async.promise.from-channel]) | |
(comment | |
;; cf. https://gist.github.com/swannodette/5888989 | |
(defn debounce | |
([c ms] (debounce (a/chan) c ms)) | |
([c' c ms] | |
(a/go | |
(loop [start nil loc (a/<! c)] | |
(if (nil? start) | |
(do | |
(>! c' loc) | |
(recur (js/Date.) nil)) | |
(let [loc (a/<! c)] | |
(if (>= (- (js/Date.) start) ms) | |
(recur nil loc) | |
(recur (js/Date.) loc)))))) | |
c')) | |
) | |
;; Using kitchen-async, this can also be written as follows: | |
(defn debounce | |
([c ms] (debounce (a/chan) c ms)) | |
([c' c ms] | |
(p/loop [start nil loc c] | |
(if (nil? start) | |
(do | |
(a/put! c' loc) | |
(p/recur (js/Date.) nil)) | |
(p/let [loc c] | |
(if (>= (- (js/Date.) start) ms) | |
(p/recur nil loc) | |
(p/recur (js/Date.) loc))))) | |
c')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment