Iterator<T> (final T elem, boolean includeHead) { if (!visited.add(elem)) { return Collections.emptyIterator(); } if (!includeHead) { return flat(map(step.fun(elem).iterator(), new Function<T, Iterator<T>>() { @Override public Iterator<T> fun(T obj) { return recurse(obj, true); } })); } Iterator<? extends T> tail = new LazyIterator<T>() { @Override protected Iterator<? extends T> create() { return step.fun(elem).iterator(); } }; return flat(asIterator(elem), flat(map(tail, new Function<T, Iterator<T>>() { @Override public Iterator<T> fun(T obj) { return recurse(obj, true); } }))); }