Where’s My for Loop?
Clojure has no for loop and no direct mutable variables.[22] So, how do you write all that code you are accustomed to writing with for loops?
Rather than create a hypothetical example, we decided to grab a piece of open source Java code (sort of) randomly, find a method with some for loops and variables, and port it to Clojure. We opened the Apache Commons project, which is very widely used. We selected the StringUtils class in Commons Lang, assuming that such a class would require little domain knowledge to understand. We then browsed for a method that had multiple for loops and local variables and found indexOfAny:
| data/snippets/StringUtils.java | |
// From Apache Commons Lang, http://commons.apache.org/lang/ | |
public ... | |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access