Structuring the Code

One nontrivial difference between Clojure and most other languages is the way that the code is structured. In imperative languages, a common practice is for different lines of code to modify shared data, where each line accessing the memory location sees the result of the previous line of code.

For example, if we have a list of integers and we wish to square each one then print the even ones, the following Python code would be perfectly valid:

 
l = [1, 2, 3, 4, 5]
 
for​ i ​in​ l
 
i = i*i
 
 
for​ i ​in​ l
 
if​ (i mod 2 == 0)
 
 
print​ l

In Clojure this interaction is explicit. Instead of creating a shared memory location and then having different functions access it sequentially, we chain functions together and ...

Get Web Development with Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.