Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.hybird.continuation;
- import java.util.Iterator;
- import org.hybird.generator.Generator;
- public class ContinuationTest
- {
- public static void main (String [] args)
- {
- // full read
- for (String s : new ExampleGenerator())
- {
- System.out.println (s);
- }
- // reading 3 out of 4 values
- System.out.println ("");
- Iterator<String> i = new ExampleGenerator().iterator ();
- for (int x = 0; x < 3 && i.hasNext (); ++x)
- System.out.println (x + ": '" + i.next () + "'");
- // generators filtering other generators
- System.out.println ("");
- for (int even : new EvenNumbersGenerator (new RangeGenerator (10)))
- {
- System.out.println (even);
- }
- }
- @Continuation
- private static class ExampleGenerator extends Generator<String>
- {
- @Override
- protected void run ()
- {
- yield ("1");
- yield ("2");
- yield ("3");
- yield ("4");
- }
- }
- @Continuation
- public static final class EvenNumbersGenerator extends Generator<Integer>
- {
- private RangeGenerator generator;
- public EvenNumbersGenerator (RangeGenerator generator)
- {
- this.generator = generator;
- }
- @Override
- protected void run ()
- {
- for (int i : generator)
- {
- if (i % 2 == 0)
- yield (i);
- }
- }
- }
- @Continuation
- public static final class RangeGenerator extends Generator<Integer>
- {
- private int to;
- public RangeGenerator (int to)
- {
- this.to = to;
- }
- @Override
- protected void run ()
- {
- for (int i = 0; i < to; i++)
- yield (i);
- }
- }
- @Continuation
- public static class FibonnaciGenerator extends Generator<Integer>
- {
- private int to;
- public FibonnaciGenerator (int to)
- {
- this.to = to;
- }
- @Override
- protected void run ()
- {
- int a = 0;
- int b = 1;
- while (a < to)
- {
- yield (a);
- int next = a + b;
- a = b;
- b = next;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment