May 2018
Beginner to intermediate
290 pages
6h 43m
English
The good news is that Clojure comes equipped with a large assortment of functions to do interesting things with sequences—so many that we can only scratch the surface in this chapter. There’s everything from butlast (give me all but the last element) to zipmap (build a map from two sequences). The bad news is that until you’re familiar with what’s available you’ll tend to reinvent the wheel. In particular, if you find yourself processing a sequence one item at a time, perhaps with loop and recur, like this:
| | (defn total-sales [books] |
| | "Total up book sales. Books maps must have :sales key." |
| | (loop [books books total 0] |
| | (if (empty? books) |
| | total |
| | (recur (next books) |
| | (+ total (:sales (first books))))))) |
consider ...
Read now
Unlock full access