Created
June 19, 2014 05:29
-
-
Save fupfin/767cdd0c127843c7b47e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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