Reduce is great, and fast. Use it more

This commit is contained in:
Dan Holmsand 2014-02-02 11:40:23 +01:00
parent 1b6b4e449e
commit e10548f8ba
2 changed files with 14 additions and 9 deletions

View File

@ -76,10 +76,10 @@
objprops)))) objprops))))
(defn map-into-array [f arg coll] (defn map-into-array [f arg coll]
(let [a (into-array coll)] (reduce (fn [a x]
(dotimes [i (alength a)] (doto a
(aset a i (f (aget a i) arg))) (.push (f x arg))))
a)) #js [] coll))
(declare as-component) (declare as-component)

View File

@ -9,11 +9,12 @@
(defn running [] @-running) (defn running [] @-running)
(defn- capture-derefed [f] (defn- capture-derefed [f]
;; TODO: Get rid of allocation.
(binding [*ratom-context* (clojure.core/atom #{})] (binding [*ratom-context* (clojure.core/atom #{})]
[(f) @*ratom-context*])) [(f) @*ratom-context*]))
(defn- notify-deref-watcher! [derefable] (defn- notify-deref-watcher! [derefable]
(when-not (or (nil? *ratom-context*)) (when-not (nil? *ratom-context*)
(swap! *ratom-context* conj derefable))) (swap! *ratom-context* conj derefable)))
(deftype RAtom [state meta validator watches] (deftype RAtom [state meta validator watches]
@ -36,8 +37,10 @@
IWatchable IWatchable
(-notify-watches [this oldval newval] (-notify-watches [this oldval newval]
(doseq [[key f] watches] (reduce-kv (fn [_ key f]
(f key this oldval newval))) (f key this oldval newval)
nil)
nil watches))
(-add-watch [this key f] (-add-watch [this key f]
(set! (.-watches this) (assoc watches key f))) (set! (.-watches this) (assoc watches key f)))
(-remove-watch [this key] (-remove-watch [this key]
@ -63,8 +66,10 @@
(-handle-change [k sender oldval newval])) (-handle-change [k sender oldval newval]))
(defn- call-watches [obs watches oldval newval] (defn- call-watches [obs watches oldval newval]
(doseq [[k wf] watches] (reduce-kv (fn [_ key f]
(wf k obs oldval newval))) (f key obs oldval newval)
nil)
nil watches))
(deftype Reaction [f ^:mutable state ^:mutable dirty? ^:mutable active? (deftype Reaction [f ^:mutable state ^:mutable dirty? ^:mutable active?
^:mutable watching ^:mutable watches ^:mutable watching ^:mutable watches