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 ...

Get Programming Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.