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))))
(defn map-into-array [f arg coll]
(let [a (into-array coll)]
(dotimes [i (alength a)]
(aset a i (f (aget a i) arg)))
a))
(reduce (fn [a x]
(doto a
(.push (f x arg))))
#js [] coll))
(declare as-component)

View File

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