Skip to content

Instantly share code, notes, and snippets.

@fupfin
Created June 19, 2014 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fupfin/767cdd0c127843c7b47e to your computer and use it in GitHub Desktop.
Save fupfin/767cdd0c127843c7b47e to your computer and use it in GitHub Desktop.
import static java.lang.System.out;
import com.google.code.jyield.*;
public class GeneratorDemo {
public static void main(String args[]) {
for(int n: YieldUtils.toIterable(take(25, sequenceOf(integers()))))
out.println(n);
}
public static Generator<Integer> take(int count, Generator<Integer> source) {
int[] cursorHolder = {0};
return y ->
source.generate(n -> {
if(cursorHolder[0] ++ < count) y.yield(n);
});
}
public static Generator<Integer> sequenceOf(Generator<Integer> source) {
return y -> source.generate(n -> y.yield(n * n));
}
public static Generator<Integer> integers() {
return y -> {
for(int current = 1; current < Integer.MAX_VALUE; current ++)
y.yield(current);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment