Exercises
The following sections have a few exercises for you.
Exercise 5.1
Extend our current EventStream
implementation to include a function called take
. It works much like Clojure's core take
function for sequences: it will take n
items from the underlying event stream after which it will stop emitting items.
A sample interaction, which takes the first five items emitted from the original event stream, is shown here:
(def es1 (from-interval 500)) (def take-es (take es1 5)) (subscribe take-es #(prn "Take values: " %)) ;; "Take values: " 0 ;; "Take values: " 1 ;; "Take values: " 2 ;; "Take values: " 3 ;; "Take values: " 4
Tip
Keeping some state might be useful here. Atoms can help. Additionally, try to think of a way to dispose of any unwanted subscriptions required by the solution.
Exercise 5.2
In this exercise, we will add a function called zip
that zips together items emitted from two different event streams into a vector.
A sample interaction with the zip
function is as follows:
(def es1 ...