In this exercise, we will add a function called zip, which zips together items that are emitted from two different event streams into a vector.
A sample interaction with the zip function is as follows:
(def es1 (from-interval 500)) (def es2 (map (from-interval 500) #(* % 2))) (def zipped (zip es1 es2)) (def token (subscribe zipped #(prn "Zipped values: " %))) ;; "Zipped values: " [0 0] ;; "Zipped values: " [1 2] ;; "Zipped values: " [2 4] ;; "Zipped values: " [3 6] ;; "Zipped values: " [4 8] (dispose token)
For this exercise, we need a way to know when we have enough items to emit from both event streams. Managing this internal state can be tricky at first. Clojure's ref types and, in particular, dosync, can be of use.