Reducing without reduce
Before we go any further, it's important to point out that sometimes there are other, better options than reduce
for taking a sequence and turning it into something non-sequential. Often, this is because Clojure provides functions that do the hard work for us. Sometimes, clever use of Clojure's "sequence-to-sequence" functions can get you the data you need.
As a general rule, it is usually preferable to do as much as possible with functions that can handle lazy sequences before turning to reduce
. In some cases, this can be for performance reasons, and in nearly all cases, your code will be easier to write, and, more importantly, to read, if you can stay in the realm of sequences. That said, most solutions will require a little of both. Knowing how to combine the two is an important skill.
zipmap
Clojure's zipmap
function is a tool for building a map from two sequences. The first sequence becomes the keys for the new map and...