Skip to content

Instantly share code, notes, and snippets.

@ku1ik
Last active August 29, 2015 14:22
Show Gist options
  • Save ku1ik/05c31e7fe98e20af8993 to your computer and use it in GitHub Desktop.
Save ku1ik/05c31e7fe98e20af8993 to your computer and use it in GitHub Desktop.
(deftest changes-at-test
(let [frames [[2 :a] [4 :b] [6 :c]]]
(is (= (changes-at frames 0) nil))
(is (= (changes-at frames 1) nil))
(is (= (changes-at frames 2) :a))
(is (= (changes-at frames 3) :a))
(is (= (changes-at frames 4) :a))
(is (= (changes-at frames 5) :a))
(is (= (changes-at frames 6) :b))
(is (= (changes-at frames 7) :b))
(is (= (changes-at frames 11) :b))
(is (= (changes-at frames 12) :c))
(is (= (changes-at frames 13) :c))))
; take 1
(defn changes-at [frames seconds]
(last
(first
(reduce
(fn [[_ t :as acc] [delay _ :as frame]]
(let [t1 (+ t delay)]
(if (> t1 seconds) (reduced acc) [frame t1])))
[nil 0]
frames))))
; take 2
(defn changes-at [frames seconds]
(let [frames-with-abs-time (reductions (fn [[t _] [delay changes]] [(+ t delay) changes]) frames)
prev-frames (take-while (comp (partial >= seconds) first) frames-with-abs-time)]
(last (last prev-frames))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment