Threading Function Calls

At some point, you will be writing sequences of collection-processing functions. Presume, for some reason, you wanted the sum of all squared integers that are less than 1000 and divisible by four.

Here is code that will work, using the range function, which generates a sequence of numbers from zero up to but not including the number you specify:

 
1
(defn multiple-of-four? [value]
2
    (= (rem value 4) 0))
3
4
(defn square [x] (* x x))
5
6
(reduce +
7
   (filter multiple-of-four?
8
      (map square (range 33))))
9

Nested calls (nested_calls)

This, in fact, mirrors the way it was described in English, but you can also see that you have to write the expression from the “inside out”. You can write the same expression using the ->> threading operator. The ->> operator takes two or more arguments. The first argument is a list, which is used as the last item in the second argument, which is an expression. If there are more arguments, the result of the second expression is used as the last item of the third argument, and so on. In other words, the original list “threads” its way through the expressions, always as the last item. This lets you rewrite the preceding code as follows:

10
 
1
(defn multiple-of-four? [value]
2
    (= (rem value 4) 0))
3
4
(defn square [x] (* x x))
5
6
(->> (range 33)
7
     (map square)
8
     (filter multiple-of-four?)
9
     (reduce +))
10

Threaded calls (threaded_calls)

This is more in line with a description of the algorithm as “Take the numbers 0 to 32, square them, get only the multiples of four, and add them up.”

Use whichever method makes the algorithm more clear to you; that will make it clear to other people—and when you come back to code you wrote a few months ago, you will be “other people.”

Next Section - Price and Discount Web Page