Skip to content

Streaming prerequisites

marick edited this page Mar 19, 2012 · 1 revision

Most functions in Clojure programs are "pure": no matter how many times, or when, you call them, they always return the same result. But that's not always true. Consider the rand function, which (probably) returns a new random number after each call.

Now suppose you're testing a function, converger, that calls (rand 0.1) n times and returns the average value. A simple test would be that (converger 2) should return 0.2 if rand first produces 0.1, then again produces 0.3. But to write that test, you need some way to describe rand returning different values on successive calls. That's done with the =streams=> arrow:

(fact 
  (converger 2) => (roughly 0.2)
  (provided
    (rand 1.0) =streams=> [0.1 0.3]))

In this particular case, it might be useful to document that rand should be called exactly two times:

(fact 
  (converger 2) => (roughly 0.2)
  (provided
    (rand 1.0) =streams=> [0.1 0.3] :times 2))

You can also use lazy sequences on the right-hand side of =streams=>. The following streams as many non-negative integers as needed by a caller of provider:

(fact
  ...
  (provided
    (provider) =streams=> (range) ))
Clone this wiki locally