Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
import java.util.*; public class Numbers implements Iterator<Integer> { int top, curr = 0; public Numbers(int n) { top = n; } public boolean hasNext() { return curr < top; } public Integer next() { return curr++; } public void remove() { throw new RuntimeException("NO"); } public static void main(String args[]) { Iterator<Integer> it = new Numbers(100); it.next(); it.next(); while (it.hasNext()) { int i = it.next(); System.out.println(i); it = new Filter(it, i); } } } class Filter implements Iterator<Integer> { Iterator<Integer> stream; Integer front; int factor; public Filter(Iterator<Integer> s, int f) { stream = s; factor = f; } public boolean hasNext() { if (front == null) { if (!stream.hasNext()) return false; front = stream.next(); } while (front % factor == 0) { if (!stream.hasNext()) return false; front = stream.next(); } return true; } public Integer next() { if (!hasNext()) throw new RuntimeException("You are dumb"); Integer rv = front; front = null; return rv; } public void remove() { throw new RuntimeException("NO"); } }
This paste will be private.
From the Design Piracy series on my blog: