Skip to content

Commit

Permalink
Define a better lift that's variable args.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeiklejohn committed Mar 20, 2013
1 parent 5279f62 commit 3935003
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/cljs/shafty/core.cljs
Expand Up @@ -30,10 +30,7 @@


(defprotocol ILiftable (defprotocol ILiftable
"Provide a mechanism for lifting functions onto behaviours." "Provide a mechanism for lifting functions onto behaviours."
(lift! [this lift-fn]) (lift! [this lift-fn]))
(lift2!
[this that lift-fn]
[this that lift-fn initial]))


(defprotocol IObservable (defprotocol IObservable
"Generate events or behaviours from browser elements or events." "Generate events or behaviours from browser elements or events."
Expand Down Expand Up @@ -103,6 +100,14 @@
"Collect values over an event stream." "Collect values over an event stream."
(-collect! coll f v)) (-collect! coll f v))


(defn lift! [f & bs]
"Lift a funciton on a behaviour."
(let [flat-bs (flatten (vec bs))
new-stream (reduce (fn [acc x] (merge! acc x)) (map changes! bs))]
(->
(-map! new-stream (fn [] (apply f (doall (map deref flat-bs)))))
(hold! nil))))

;; ;;
;; Pulses ;; Pulses
;; ;;
Expand Down Expand Up @@ -165,6 +170,9 @@
(set! (.-sinks this) (vec (filter (fn [x] (= x that)) (.-sinks this)))) (set! (.-sinks this) (vec (filter (fn [x] (= x that)) (.-sinks this))))
this) this)


IEventConversion
(changes! [this] this)

IRequestable IRequestable
(requests! [this] (requests! [this]
(let [xhr (xhr-connection) (let [xhr (xhr-connection)
Expand Down Expand Up @@ -346,13 +354,7 @@


ILiftable ILiftable
(lift! [this lift-fn] (lift! [this lift-fn]
(-> (changes! this) (-map! lift-fn) (hold! nil))) (-> (changes! this) (-map! lift-fn) (hold! nil))))
(lift2! [this that lift-fn]
(lift2! this that lift-fn nil))
(lift2! [this that lift-fn initial]
(-> (merge! (changes! this) (changes! that))
(-map! (fn [] (apply lift-fn [@this @that])))
(hold! initial))))


;; ;;
;; Priority Map ;; Priority Map
Expand Down

0 comments on commit 3935003

Please sign in to comment.