Also, this bit:<p><pre><code> (->> justice-league
(map #(:vehicles %))
(filter #(not (nil? %)))
(flatten))
</code></pre>
can be simplified to just `(mapcat :vehicles justice-league)`
Not a lot of comments here for the number of upvotes, but the trend of the comments strikes me as lopsidedly negative and I'd like to throw in something positive to the mix - Clojure needs more people publishing beginner friendly code even if it isn't using some specific function that does the same thing with less typing. So, y'know, I'm really glad we've got people out there who are documenting how they manage to get things to work under the new and generally unfamiliar paradigm of "practically functional". Especially since Clojure's Achilles heel is the combined double-whammy of its incoherent error messages and the learning curve for a beginner trying to pick up how to do common tasks in a world with 'no variables'.<p>We have a programmer with >15 years experience, who is learning a new language and in the process producing code that is neat, functional (in the sense of no bugs) and not unreasonably verbose. Clojure is a language that is dense with clever ideas, it takes a lot longer than a few months to get good at using them if they are new. The terse comments throwing out improvements or making corrections seem a little harsh, I'd hoped to see people talking more about concepts than naming functions.
The equivalent of `flatMap` is not `flatten`. Semantically `flatMap` is equivalent to `(comp flatten map)` – hence the name! – but Clojure does offer it as a single function named `mapcat` (”map and catenate”).
Out of curiosity, can anybody comment whether the Clojure versions of these are "lazy" in the sense that if you only take the first element from the result, does the the stream process all the results or just the first? eg:<p><pre><code> (first (map #(extract-name %) justice-league))
</code></pre>
Does it map all the elements or only the first? This is actually the key aspect of streams, and more important in many ways than whatever syntax sugar is available to invoke the operations.
It seems strange to me that
`(map extract-name justice-league)`
isn't listed under "idiomatic improvements." Supposing you already had `extract-name` defined for some reason, it's cleaner and simpler to just map that unary function than wrap that function in an anonymous function - whether that wrapping is done with the `#(...)` reader syntax or explicitly constructed with `fn`.