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

Pages: 1-4041-8081-

/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: VIPPER 2011-04-10 10:08

>>1
Are banned:
This is not acceptable for /prog/ challenges.

Name: Anonymous 2011-04-10 10:52

>>2
As if you knew how to code. Go fuck an autistic nigger.

Name: Anonymous 2011-04-10 11:26

what is a generator?

Name: Anonymous 2011-04-10 11:49

what's aNIGGER?

Name: Anonymous 2011-04-10 13:23

>>5

It's a derogatory term usually referring to African Americans.

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())

Name: nambla_dot_org 2011-04-10 20:16

>>7
Again, your Python is just some lame subsitute for a real functional programming language.

Name: Anonymous 2011-04-10 20:57

>>8
That looks like F#, not FIOC.

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.

Name: Anonymous 2011-04-10 23:38

// JavaScript
var i = 0;

generator = function(i) {
  return i++;
}

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.

Name: Anonymous 2011-04-11 1:14

>>11
That's not a generator.

Name: Anonymous 2011-04-11 3:42

>>9
What is ``FIOC''?

Name: Anonymous 2011-04-11 3:52

>>14
THE FORCED INDENTATION OF CODE. Thread continues.

Name: Anonymous 2011-04-11 4:16

By generators, do you mean co-routines, OP?

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!''

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.

Name: Anonymous 2011-04-11 4:46

>>8
Since when did Python have braces, dingus?

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.

Name: Anonymous 2011-04-11 4:55

>>19,20
He didn't mention Python explicitly, he said language native generators.

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.

Name: Anonymous 2011-04-11 9:39

Name: Anonymous 2011-04-11 13:21

>>19

if whatever: #{
    do_shit()
#}


happy now, C programmers?

Name: Anonymous 2011-04-11 14:17

>>24

if whatever: #{ do_shit() #}
if whatever:
#{
do_shift()
#}


No.

Name: Anonymous 2011-04-11 14:26

>>19

dictionary = {"since" : "ever"}

Name: Anonymous 2011-04-12 1:11

pump

Name: Anonymous 2011-04-12 1:26

>>1
This is the worst challenge I have ever seen.

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());
        }
    }
}

Name: Anonymous 2011-04-12 2:07

>>29
Ohh, I see. He meant factorials.

Name: Anonymous 2011-04-12 2:22

>>29
Is that how a generator works? I'm so confused.

Name: Anonymous 2011-04-12 2:45

>>29
Looks great, but fails to generate infinitely large factorials.

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))

Name: Anonymous 2011-04-12 3:24

>>33
s/about/about it/

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?

Name: HotCupOfJava !!7YfDQjROXukLpYb 2011-04-12 3:52

>>35
Instead of writing your method as usual, you simply extend the Generator class.

Name: Anonymous 2011-04-12 3:55

>>36
That is some straight up ENTERPRIZE bullshit right there.

Name: Anonymous 2011-04-12 4:00

>>35
Terrible!

Name: Anonymous 2011-04-12 4:02

>>36
Wow, I guess Java has some advantages after all.

Name: Anonymous 2011-04-12 4:04

>>35
Learn to understand OO.

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

­­

Name: Anonymous 2012-07-12 14:28

­­­

Name: Anonymous 2012-07-12 14:29

­­­­

Name: Anonymous 2012-07-12 14:29

­­­­­

Name: Anonymous 2012-07-12 14:29

bump

Name: Anonymous 2012-07-12 14:30

Name: Anonymous 2012-07-12 14:30

Name: Anonymous 2012-07-12 14:31

Name: Anonymous 2012-07-12 14:31

░░░░░░░░░░░░░░░▄▀▄░░░░░░░░░░░░░░░
░░░░░░░░░░░░░▄▀░░░▀▄░░░░░░░░░░░░░
░░░░░░░░░░░▄▀░░░░▄▀█░░░░░░░░░░░░░
░░░░░░░░░▄▀░░░░▄▀░▄▀░▄▀▄░░░░░░░░░
░░░░░░░▄▀░░░░▄▀░▄▀░▄▀░░░▀▄░░░░░░░
░░░░░░░█▀▄░░░░▀█░▄▀░░░░░░░▀▄░░░░░
░░░▄▀▄░▀▄░▀▄░░░░▀░░░░▄█▄░░░░▀▄░░░
░▄▀░░░▀▄░▀▄░▀▄░░░░░▄▀░█░▀▄░░░░▀▄░
░█▀▄░░░░▀▄░█▀░░░░░░░▀█░▀▄░▀▄░▄▀█░
░▀▄░▀▄░░░░▀░░░░▄█▄░░░░▀▄░▀▄░█░▄▀░
░░░▀▄░▀▄░░░░░▄▀░█░▀▄░░░░▀▄░▀█▀░░░
░░░░░▀▄░▀▄░▄▀░▄▀░█▀░░░░▄▀█░░░░░░░
░░░░░░░▀▄░█░▄▀░▄▀░░░░▄▀░▄▀░░░░░░░
░░░░░░░░░▀█▀░▄▀░░░░▄▀░▄▀░░░░░░░░░
░░░░░░░░░░░░░█▀▄░▄▀░▄▀░░░░░░░░░░░
░░░░░░░░░░░░░▀▄░█░▄▀░░░░░░░░░░░░░
░░░░░░░░░░░░░░░▀█▀░░░░░░░░░░░░░░░

Name: Anonymous 2012-07-12 15:33

make_counter = (max) ->
  current = 0
  return () ->
          if current <= max
            return current++

c = make_counter 5

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

Name: Anonymous 2012-07-14 23:46

bump

Name: Anonymous 2012-07-16 0:16

>>52
holy fucking ENTERPRISE variables nigger

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