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:07

>>36
Fuck Java. You should be disqualified for your insolence.

Name: Anonymous 2011-04-12 4:09

>>41
>>1
infinite generator
>>29
i <= n
He doesn't even qualify.

Name: Anonymous 2011-04-12 4:11

>>36-40
Samefag and we have been trolled constantly.

Name: Anonymous 2011-04-12 4:13

>>42
Take a closer look at his code. Note the two different classes.

Name: Anonymous 2011-04-12 4:14

>>29

Why the fuck do you recompute every factorial below n, every time you call generateNext()?

Terrible!

Name: Anonymous 2011-04-12 4:14

>>44
That's even worse.

Name: Anonymous 2011-04-12 4:16

>>45
Because he's a Java programmer, and Java programmers are retarded. Obviously?

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 4:16

>>43
Just, no. IHBT

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 4:19

>>45
Yes, I could have kept a variable that contained the factorial before it. I didn't care cause it was just a test method.

Name: Anonymous 2011-04-12 4:19

>>48

Go away.

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 4:23

>>50
Fuck off you anus.

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.

Name: Anonymous 2011-04-12 4:24

>>51

Go away.

Name: Anonymous 2011-04-12 4:25

>>52
And of course, Shitchan must fuck with my indentation.

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 4:34

>>52
I programmed it that way so the entire factorial function would be encapsulated into one method. Its encapsulated because that's what you would have to do in order to extend the Generator class and implement a new value generating method. You've got to think like an enterprise businessman here.

Name: Anonymous 2011-04-12 4:35

It's not infinite anyway, so who the fuck cares.

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 4:37

>>56
Yeah, you've got me there. I'm going to sleep but I might fix it tomorrow.

Name: Anonymous 2011-04-12 4:40

>>55
Recalculating the factorial every step instead of saving the state of the computation nullifies the whole point of having a generator.

>>56
What? >>52 is.

Name: Anonymous 2011-04-12 4:42

>>58
No its not. Integer limit.

Name: Anonymous 2011-04-12 4:44

>>59
*If Java didn't suck, it would.

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 5:06

Ok, I lied, I didn't go to sleep quite yet. I retooled my program to address the valid concerns of >>58 while keeping encapsulation.

public class Generator {

    int counter;
    int bound;
    int[] list;
   
    public Generator(){
        counter = 0;
        bound = 100;
        list = new int[bound];
    }
   
    public int generateNext(){
        if(counter + 1 == bound)
            reallocate();
        return facts(counter++);
    }
   
    private void reallocate() {
        int i;
        int[] temp = list;
        bound = bound * 2;
        list = new int[bound];
        for(i = 0; i < bound/2; i++)
            list[i] = temp[i];
    }

    public int facts(int n){
        if(n == 0)
            return list[counter] = 1;
        else
            return list[counter] = n * list[counter-1];
    }
}

public class GeneratorTester {
    public static void main(String args[]){
        int i;
        Generator gen = new Generator();
       
        for(i = 0; i < 200; i++){
            System.out.println(gen.generateNext());
        }
    }
}

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 5:10

>>61
That 200 in the loop is supposed to be a 5, I was checking the reallocation method and forgot to switch the magic number.

Name: Anonymous 2011-04-12 5:12

>>61
٩๏̯͡๏)۶

Name: Anonymous 2011-04-12 5:19

>>61
This is proof that java drives people university educated code monkeys insane.

Name: Anonymous 2011-04-12 5:24

>>61
W-what...

Name: Anonymous 2011-04-12 12:52

>>61
Oh, I get it. He is storing generated values in an array and making that array dynamically larger.

Regardless, still not 'infinite' due to integer limits.

Name: Anonymous 2011-04-12 12:57

Regardless, still not 'jewish' due to finite limits.

FTFY

Name: Anonymous 2011-04-12 13:04

>>67
Put down the stick, the horse is dead.

Name: Anonymous 2011-04-12 13:06

>>33
I can't seem to get this to work properly.

Name: Anonymous 2011-04-12 13:54

>>69
Are you sure you know how to use call/cc, lexical scoping and first-class closures to your advantage?

Name: Anonymous 2011-04-12 13:54

>>69
Read his damn post he said it didn't work right.

Name: Anonymous 2011-04-12 13:57

>>69
No, I don't even know what a first-class closure is. I'm still new to functional programming.

Name: Anonymous 2011-04-12 14:01

Whats with all the autistic hostility on this thread(and all of /prog/)? Self-esteem issues?

Name: ormaaj 2011-04-12 14:45

take 5 $ map (fix (\f x -> if x == 0 then 1 else x * f (x - 1))) [1..]

Really the best answer to simulate a generator would be corecursive.

Name: Anonymous 2011-04-12 15:30

Classic producer consumer dilemma.

Name: Anonymous 2011-04-12 20:28

ITT: People making up their own definitions of what a ``generator'' is.

Name: Anonymous 2011-04-15 16:39

RETURN 1;

Name: Anonymous 2011-04-18 1:50

>>61
Complete, small and easy to use, you win.

Tis a sad day for /prog/

Name: Anonymous 2012-07-12 14:27

­

Name: Anonymous 2012-07-12 14:27

­­

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