2 kyu

Generator Functions

Description
Loading description...
Iterators
Functional Programming
Fundamentals
  • Please sign in or sign up to leave a comment.
  • kodownik Avatar

    Interesting task!

    (Note: generator functions as presented in this problem slightly diverge from generator functions in actual JS, but it's a little simpler this way.)

    To me it's the opposite. I think this makes it more complex, because the first next call behaves differently from subsequent ones.

  • hobovsky Avatar

    Regarding this question: https://www.codewars.com/kata/59e7c7e5fc3c49d93f0000d3/discuss#605e6ee0983870002f0cfabb

    This should be explained in the description. I was scrathing my head since the first time I saw this kata and wondered how to squeeze generators in a way they do not get in a way of each other into required API of Flow and could not come up with anything, just to read here that it's not necessary.

    The fact that this requirement does not have to be satisfied is counter-intuitive because now the kata relies on the fact that generators are not fully independent.

  • myplacedk Avatar

    This comment has been hidden.

  • myplacedk Avatar

    This comment has been hidden.

  • dfhwze Avatar

    If the 'next' method of the generator takes a parameter, it's not clear to me how the flow should be managed.

    The first call of 'next' should provide the initial value to the generator method and return that value -> ok. In case of the summation example 'next(10)' -> returns 10 -> 'total' = 10 Then the second call 'next(42)' -> the yield should take this parameter and provide it to the generator function: - so total becomes 10 + 42 = 52 - then, that second call should return 52. How is this possible if 'total' is a local variable inside the generator function and it got updated to 52 after the yield took place?

  • dfhwze Avatar

    Should it be possible for multiple generators to yield results in different threads that run in parallel?

  • JohanWiltink Avatar

    This is approvable.

    I know JS and generator functions there; I don't know Java.

    Is this really 1 or 2 kyu?

  • pcr3w Avatar

    I had a little look at this Kata today and have a solution that solves the sample tests fine, but times out for the final tests. I'm finding it a little hard to find the root of the problem, as I'm unable to debug the cases. Any help would be greatly appreciated!

  • DasBrain Avatar

    Ok, just so I get this right. In the example test, there is this GeneratorFunction :

    funcB = new GeneratorFunction<>(() -> {
      Flow.yield("lorem");
      Flow.yieldFrom(funcA);
      Flow.yield("sit");
      Flow.yield("amet");
      Flow.yield("consectetur");
    });
    

    After the call to next() which returns "consectetur" I need a new value to continue running the Generator.

    Fix it by adding this line in the sample tests.

    Assert.assertEquals(null, gen.next());
    

    (Also read as: I pass the final tests, but not the sample tests.)

  • Javatlacati Avatar

    In the description it has the sample code

    GeneratorFunction<Integer, Integer> summation = new GeneratorFunction<>(initialValue -> {
      int total = initialValue;
      while (true) {
        total += Flow.yield(total);
      }
    });
    Generator<Integer, Integer> gen = summation.call();
    System.out.println(gen.next(10)); // 10
    System.out.println(gen.next(42)); // 52
    System.out.println(gen.next(17)); // 69
    System.out.println(gen.next(638)); // 707
    

    wich will result in Error:

    java: bad operand types for binary operator '+' first type: int second type: java.lang.Object

    It would be better to make the code like in the unit test

    GeneratorFunction<Integer, Integer> summation = new GeneratorFunction<>(initialValue -> {
      int total = initialValue;
      while (true) {
        total += Flow.<Integer, Integer>yield(total);
      }
    });
    Generator<Integer, Integer> gen = summation.call();
    System.out.println(gen.next(10)); // 10
    System.out.println(gen.next(42)); // 52
    System.out.println(gen.next(17)); // 69
    System.out.println(gen.next(638)); // 707
    

    hence please update description.

  • JohanWiltink Avatar

    yield statement

    yield*, or yield-from statement

    In JavaScript, yield is actually an operator, and yield total is an expression.

  • Blind4Basics Avatar

    Hi,

    Could you provide another sample test using the Consumer and where the generator isn't infinite?

  • ZED.CWT Avatar

    Tricky one. One thing that the compiler seems not happy with total += Flow.yield(total); in the example tests. // total += (int)Flow.yield(total); will make it happy, but I do not know why IDEA tells that (int) is redundant.