Skip to content

Instantly share code, notes, and snippets.

@rafd
Created December 8, 2022 02:17
Show Gist options
  • Save rafd/c70dc137ce4be7090680526a29f77846 to your computer and use it in GitHub Desktop.
Save rafd/c70dc137ce4be7090680526a29f77846 to your computer and use it in GitHub Desktop.
2022-12-07 clojure workshop leaky bucket
(ns demo.core)
;; inputs are "litres/s"
;; bucket is leaking 1 l/s
;; bucket has capacity of 10
;; does the input overflow the bucket?
(defn leaky-bucket-overflowing?
[limit list]
(->> list
(map dec)
(reductions +)
(some #(< limit %))
boolean))
(defn leaky-bucket-overflowing?
[limit list]
(let [level (atom 0)
overflowed? (atom false)]
(doseq [entity list]
(swap! level + entity)
(swap! level dec)
(when-not @overflowed?
(reset! overflowed? (< limit @level))))
@overflowed?))
(defn leaky-bucket-overflowing?
[limit list]
(loop [level 0
items list]
(cond
(< limit level)
true
(empty? items)
false
:else
(recur (+ level (first items) -1)
(rest items)))))
#_(leaky-bucket-overflowing? 10 [12]) ;; true
#_(leaky-bucket-overflowing? 10 [1 1 1 1 1 1 1 1]) ;; false
#_(leaky-bucket-overflowing? 10 [10 0 0 0 0 0 3]) ;; false
#_(leaky-bucket-overflowing? 10 [12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]) ;; true
#_(leaky-bucket-overflowing? 10 [2 0 0 0 0 0 0 2 1 3 1 0 0 0 1 1 1 0 1]) ;; false
;; vs-code
;; alt-enter - evaluate the current top level form (even if it's a comment)
;; ctrl-s - save file, require the entire file (load into REPL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment