/prog/ challenge (S(SKK)(SKK))(S(SKK)(SKK))
1
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.
41
Name:
Anonymous
2011-04-12 4:07
>>36
Fuck Java. You should be disqualified for your insolence.
42
Name:
Anonymous
2011-04-12 4:09
>>41
>>1
infinite generator
>>29
i <= n
He doesn't even qualify.
43
Name:
Anonymous
2011-04-12 4:11
>>36-40
Samefag and we have been trolled constantly.
44
Name:
Anonymous
2011-04-12 4:13
>>42
Take a closer look at his code. Note the two different classes.
45
Name:
Anonymous
2011-04-12 4:14
>>29
Why the fuck do you recompute every factorial below n, every time you call
generateNext()?
Terrible
!
46
Name:
Anonymous
2011-04-12 4:14
47
Name:
Anonymous
2011-04-12 4:16
>>45
Because he's a Java programmer, and Java programmers are retarded. Obviously?
48
Name:
HotCupOfJava
!!7YfDQjROXukLpYb
2011-04-12 4:16
49
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.
50
Name:
Anonymous
2011-04-12 4:19
51
Name:
HotCupOfJava
!!7YfDQjROXukLpYb
2011-04-12 4:23
52
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.
53
Name:
Anonymous
2011-04-12 4:24
54
Name:
Anonymous
2011-04-12 4:25
>>52
And of course, Shi
t chan
must fuck with my indentation.
55
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.
56
Name:
Anonymous
2011-04-12 4:35
It's not infinite anyway, so who the fuck cares.
57
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.
58
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.
59
Name:
Anonymous
2011-04-12 4:42
>>58
No its not. Integer limit.
60
Name:
Anonymous
2011-04-12 4:44
>>59
*If Java didn't suck, it would.
61
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());
}
}
}
62
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.
63
Name:
Anonymous
2011-04-12 5:12
64
Name:
Anonymous
2011-04-12 5:19
>>61
This is proof that java drives
people university educated code monkeys insane.
65
Name:
Anonymous
2011-04-12 5:24
66
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.
67
Name:
Anonymous
2011-04-12 12:57
Regardless, still not 'jewish' due to finite limits.
FTFY
68
Name:
Anonymous
2011-04-12 13:04
>>67
Put down the stick, the horse is dead.
69
Name:
Anonymous
2011-04-12 13:06
>>33
I can't seem to get this to work properly.
70
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?
71
Name:
Anonymous
2011-04-12 13:54
>>69
Read his damn post he said it didn't work right.
72
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.
73
Name:
Anonymous
2011-04-12 14:01
Whats with all the autistic hostility on this thread(and all of /prog/)? Self-esteem issues?
74
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.
75
Name:
Anonymous
2011-04-12 15:30
Classic producer consumer dilemma.
76
Name:
Anonymous
2011-04-12 20:28
ITT: People making up their own definitions of what a ``generator'' is.
77
Name:
Anonymous
2011-04-15 16:39
RETURN 1;
78
Name:
Anonymous
2011-04-18 1:50
>>61
Complete, small and easy to use, you win.
Tis a sad day for /prog/
79
Name:
Anonymous
2012-07-12 14:27
80
Name:
Anonymous
2012-07-12 14:27
Newer Posts