A Transformer
interface Function {
Object fn(Object o);
}
class TransformIterator implements Iterator {
public TransformIterator(Iterator iter, Function tx) ...
public boolean hasNext() { return iter_.hasNext(); }
public Object next() {
return tx_.fn(iter_.next());
}
public void remove() {
iter_.remove();
}
...
}
We can have some source - a list, a database, a file -, throw an iterator around it, filter it, process it, filter again maybe, merge in another source, and so on and so on.
It's another tool, another idea.
An iterator that wraps another iterator - like the filter - starts to make iterators composable. The predicates, too, are composable. When people start throwing words like "composable" around, you often fear being berated for not writing in Lisp and heading off to some wonderful functional programming future.
Now I'm not going to pitch you the dream of building software out of lego bricks. I'm just trying to highlight these ideas, and suggest that there are places where this stuff is not just appropriate, but can radically simplify and clarify.