#summary A few examples. #labels Phase-Support = Introduction = These are a few examples of simple iterators created using the _yield_ feature. These are implemented by having a static method in a utility class, which supplies iterators for different needs. == Filtering Iterator == {{{ public interface Predicate { boolean evaluate(T item); } public static Iterable filteredIterable(Iterable it, Predicate pred) { return new Yielder() { public void yieldNextCore() { for (T item : it) { if (pred.evaluate(item)) { yieldReturn(item); } } }; } }}} == Transforming Iterator == {{{ public interface Transformation { K transform(T item); } public static Iterable transformedIterable(Iterable it, Transformation trans) { return new Yielder() { public void yieldNextCore() { for (T item : it) { yieldReturn(trans.transform(item)); } }; } }}} == DFS on a Graph == {{{ public interface GraphNode { Iterable getNeighbours(); } public static Iterable depthFirstSearch(GraphNode graph) { return dfsCore(graph, new HashSet()); } private static Iterable dfsCore(GraphNode node, Collection visited) { return new Yielder() { protected void yieldNextCore() { if (visited.contains(node)) { yieldBreak(); } yieldReturn(node); visited.add(node); // Now, let's iterate the neighbours and recursively call DFS. for (Object neighbour :node.getNeighbours()) { for (Object yieldedNeighbour : dfsCore(neighbour, visited)) { yieldReturn(yieldedNeighbour); } } } }; } }}}