/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.
2
Name:
VIPPER
2011-04-10 10:08
>>1
Are banned:
This is not acceptable for /prog/ challenges.
3
Name:
Anonymous
2011-04-10 10:52
>>2
As if you knew how to code. Go fuck an
autistic nigger .
4
Name:
Anonymous
2011-04-10 11:26
what is a generator?
5
Name:
Anonymous
2011-04-10 11:49
what's aNIGGER?
6
Name:
Anonymous
2011-04-10 13:23
>>5
It's a derogatory term usually referring to African Americans.
7
Name:
Anonymous
2011-04-10 15:55
open System
let facts = seq {
while true do
yield "MY ANUS"
yield "MY AUTISM"
}
let compute n = Console.WriteLine("HAX {0}!", Seq.nth n facts)
for n = 1 to 5 do
compute n
ignore(Console.ReadKey())
8
Name:
nambla_dot_org
2011-04-10 20:16
>>7
Again, your Python is just some lame subsitute for a real functional programming language.
9
Name:
Anonymous
2011-04-10 20:57
>>8
That looks like F#, not FIOC.
10
Name:
Anonymous
2011-04-10 21:06
>>6
Nigger just means a black person, though people do use nigger as a derogative term for a black person.
11
Name:
Anonymous
2011-04-10 23:38
// JavaScript
var i = 0;
generator = function(i) {
return i++;
}
12
Name:
Anonymous
2011-04-10 23:41
>>10
``Nigger'' was derogatory since they started to use it. Only after African Americans started to fight for their rights did the word usage seize, and only then did African Americans start to use it among each other ``endearingly''. Either way, it has never been ``just another word'' for people.
... other than on 4chan and the rest of the fine places on the internet, of course.
13
Name:
Anonymous
2011-04-11 1:14
>>11
That's not a generator.
14
Name:
Anonymous
2011-04-11 3:42
15
Name:
Anonymous
2011-04-11 3:52
>>14
THE FORCED INDENTATION OF CODE. Thread continues.
16
Name:
Anonymous
2011-04-11 4:16
By generators, do you mean co-routines, OP?
17
Name:
Anonymous
2011-04-11 4:22
>>10
http://www.youtube.com/watch?v=i-Fig_LoBy4
``Take your filthy black hands off me nigger!''
18
Name:
Anonymous
2011-04-11 4:43
>>16
Not exactly, coroutines run cooperatively (hence the name), and yield
the control to another procedure, but may or not may return a value.
Generators, instead, yield
a value to the caller, maintaining the state of the computation between calls.
I'm sure that you can implement generators in terms of coroutines, but they are not the same.
19
Name:
Anonymous
2011-04-11 4:46
>>8
Since when did Python have braces, dingus?
20
Name:
Anonymous
2011-04-11 4:54
>>8
It looks like F# for the .NET Framework, but it's using the same underlying .NET/CLI yield mechanism that C# uses. Perhaps you should have been more clear about this.
21
Name:
Anonymous
2011-04-11 4:55
>>19,20
He didn't mention Python explicitly, he said language native generators.
22
Name:
Anonymous
2011-04-11 5:00
>>20
If your language supports ``natively'' (or, has a special syntax/data structure/something for it), you can't use it (how did I logic?).
Also, ignore nambla_dot_org's troll posts.
23
Name:
Anonymous
2011-04-11 9:39
24
Name:
Anonymous
2011-04-11 13:21
>>19
if whatever: #{
do_shit()
#}
happy now,
C programmers?
25
Name:
Anonymous
2011-04-11 14:17
>>24
if whatever: #{ do_shit() #}
if whatever:
#{
do_shift()
#}
No.
26
Name:
Anonymous
2011-04-11 14:26
>>19
dictionary = {"since" : "ever"}
27
Name:
Anonymous
2011-04-12 1:11
pump
28
Name:
Anonymous
2011-04-12 1:26
>>1
This is the worst challenge I have ever seen.
29
Name:
HotCupOfJava
!!7YfDQjROXukLpYb
2011-04-12 1:58
If I have the only non anus related entry by deadline, I'm quitting /prog/.
public class Generator {
int counter;
public Generator(){
counter = 0;
}
public int generateNext(){
return facts(counter++);
}
public int facts(int n){
int i, nthFact = 1;
for(i = 1; i <= n; i++){
nthFact = nthFact * i;
}
return nthFact;
}
}
public class GeneratorTester {
public static void main(String args[]){
int i;
Generator gen = new Generator();
for(i = 0; i < 5; i++){
System.out.println(gen.generateNext());
}
}
}
30
Name:
Anonymous
2011-04-12 2:07
>>29
Ohh, I see. He meant factorials.
31
Name:
Anonymous
2011-04-12 2:22
>>29
Is that how a generator works? I'm so confused.
32
Name:
Anonymous
2011-04-12 2:45
>>29
Looks great, but fails to generate infinitely large factorials.
33
Name:
Anonymous
2011-04-12 3:23
Ok, I tried to do it the ``proper'' way, without using macros and shit, but then I realized that I couldn't without having access to the generator procedure's scope or doing some kind of black magic (or implement yield as procedure/macro).
So, think about this way: to maintain the purity of the computation done by the procedure, ``yield'' returns the next ``yield''.
This permits the generator to generally fuck up with the whole program.
(define (gen f)
(define ((resume-of k y) kk)
(k (y kk)))
(define ((continue kk) . x)
(let/cc k
(set! resume (resume-of k continue))
(apply kk x)))
(define resume
(resume-of f continue))
(lambda x
(call/cc resume)))
(define facts
(gen (lambda (yield)
(let loop ((yield yield)
(n 1) (r 1))
(loop (yield r)
(+ n 1) (* n r))))))
(for/list ((i (in-range 0 5)))
(facts))
34
Name:
Anonymous
2011-04-12 3:24
35
Name:
Anonymous
2011-04-12 3:46
>>29
I don't get it, how do you do anything else with that generator other than generate factorials?
36
Name:
HotCupOfJava
!!7YfDQjROXukLpYb
2011-04-12 3:52
>>35
Instead of writing your method as usual, you simply extend the Generator class.
37
Name:
Anonymous
2011-04-12 3:55
>>36
That is some straight up
ENTERPRIZE bullshit right there.
38
Name:
Anonymous
2011-04-12 4:00
39
Name:
Anonymous
2011-04-12 4:02
>>36
Wow, I guess Java has some advantages after all.
40
Name:
Anonymous
2011-04-12 4:04
>>35
Learn to understand OO.
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
81
Name:
Anonymous
2012-07-12 14:28
82
Name:
Anonymous
2012-07-12 14:29
83
Name:
Anonymous
2012-07-12 14:29
84
Name:
Anonymous
2012-07-12 14:29
bump
85
Name:
Anonymous
2012-07-12 14:30
86
Name:
Anonymous
2012-07-12 14:30
87
Name:
Anonymous
2012-07-12 14:31
88
Name:
Anonymous
2012-07-12 14:31
░░░░░░░░░░░░░░░▄▀▄░░░░░░░░░░░░░░░
░░░░░░░░░░░░░▄▀░░░▀▄░░░░░░░░░░░░░
░░░░░░░░░░░▄▀░░░░▄▀█░░░░░░░░░░░░░
░░░░░░░░░▄▀░░░░▄▀░▄▀░▄▀▄░░░░░░░░░
░░░░░░░▄▀░░░░▄▀░▄▀░▄▀░░░▀▄░░░░░░░
░░░░░░░█▀▄░░░░▀█░▄▀░░░░░░░▀▄░░░░░
░░░▄▀▄░▀▄░▀▄░░░░▀░░░░▄█▄░░░░▀▄░░░
░▄▀░░░▀▄░▀▄░▀▄░░░░░▄▀░█░▀▄░░░░▀▄░
░█▀▄░░░░▀▄░█▀░░░░░░░▀█░▀▄░▀▄░▄▀█░
░▀▄░▀▄░░░░▀░░░░▄█▄░░░░▀▄░▀▄░█░▄▀░
░░░▀▄░▀▄░░░░░▄▀░█░▀▄░░░░▀▄░▀█▀░░░
░░░░░▀▄░▀▄░▄▀░▄▀░█▀░░░░▄▀█░░░░░░░
░░░░░░░▀▄░█░▄▀░▄▀░░░░▄▀░▄▀░░░░░░░
░░░░░░░░░▀█▀░▄▀░░░░▄▀░▄▀░░░░░░░░░
░░░░░░░░░░░░░█▀▄░▄▀░▄▀░░░░░░░░░░░
░░░░░░░░░░░░░▀▄░█░▄▀░░░░░░░░░░░░░
░░░░░░░░░░░░░░░▀█▀░░░░░░░░░░░░░░░
89
Name:
Anonymous
2012-07-12 15:33
make_counter = (max) ->
current = 0
return () ->
if current <= max
return current++
c = make_counter 5
90
Name:
Anonymous
2012-07-13 19:22
PROC facs generator = PROC INT: (
HEAP INT g := 1, n := 0;
INT: g *:= (n +:= 1));
PROC INT fac = facs generator;
TO 5 DO print((fac, new line)) OD
91
Name:
Anonymous
2012-07-14 23:46
bump
92
Name:
Anonymous
2012-07-16 0:16
>>52
holy fucking
ENTERPRISE variables nigger