-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
The Clojure Workshop
By :
Before we move on, it's important to take a closer look at how lazy sequences work in Clojure. When using map and filter, lazy evaluation is often an important consideration. In the examples we've looked at so far, we have used a literal vector as input: [1 2 3 4 5]. Instead of typing out each number, we could use the range function and write (range 1 6). If we type this in the REPL, we get basically the same thing:
user> (range 1 6) (1 2 3 4 5)
So, is this just a shortcut to avoid typing out lots of integers? Well, it is, but range has another interesting characteristic: it's lazy.
Before we go further, let's revisit laziness briefly. If (range 100) is a lazy sequence, that means that it is not realized until each element in the sequence has been calculated. Say we define a lazy sequence from 0 to 100:
user> (def our-seq (range 100))
Note
The REPL causes lazy sequences to be evaluated. This can be confusing sometimes...