jawher/moulder-j — pull request #2 by itoshkov (2012-11-08 13:04:24+00:00)
Title: New moulder: forEach

Following my questions from issue #1 I was looking for an alternative to the `repeat` pattern. My idea is to use something similar to the for-each construct.

I hacked a simple implementation, which can be invoked like this:

```java
List items = Arrays.asList("summer", "autumn", "winter", "spring");

SimpleValue val = new SimpleValue(null);

m.register("h1",
forEach(val, seq(items),
text(val),
attr("class", seq("even", "odd").cycle()),
append("

content

")));
```

The first parameter to `forEach` should be a `SimpleValue`. Its value "traverses" the list (`SimpleValue` is now mutable). The second parameter is a (finite) `SeqValue`. The rest of the parameters are the "body" of the loop.

Pros:
1. Composable. That is, you can have nested loops.
1. It's also possible to create other similar constructs.

Cons:
1. Requires a mutable value (but JSoup's DOM is already mutable, so one has to be careful anyway)
1. It feels like implementing an embedded language, instead of just using Java (I wish Java was Lisp ;))

What do you think?