Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ challenge (S(SKK)(SKK))(S(SKK)(SKK))

Name: Anonymous 2011-04-10 9:36

The challenge: Implement generators in the favourite language of yours.
Are banned: Language native generators (i.e. FIOC's yield), delimited continuations (sorry, Schemers, that would be too easy).

The test program will be an infinite generator that computes facts, and a loop that takes the first five of them.

The deadline is 17/04/2011 00:00 /prog/time.
The most complete, small and easy to use implementation wins.

Name: Anonymous 2011-04-12 4:23

>>47
Indeed, I'm not a Java programmer, but I can write better Java code than him.

public interface Generator<T> {
    public T yield();
}

public class FactGenerator implements Generator<int> {
    private int r;
    private int n;

    public FactGenerator() {
    r = 1;
    n = 1;
    }

    public int yield() {
    r *= n++;
    return r;
    }
}

public class HaxMyAnus {
    public static void main(String args[]) {
    int iGenericForLoopFrom0To5IndexVariable;
    FactGenerator factGenerator = new FactGenerator();
    for (iGenericForLoopFrom0To5IndexVariable = 0;
         iGenericForLoopFrom0To5IndexVariable < 5;
         iGenericForLoopFrom0To5IndexVariable++) {
        System.out.println(factGenerator.yield());
    }
    }
}


Not sure if it compiles, but that's the idea.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List