Coder Social home page Coder Social logo

Comments (10)

inxomnyaa avatar inxomnyaa commented on June 28, 2024 1

i have no idea where this comes from ^^

Offtoppic: can you explain me yield/generators?
For example, i don't understand why yield is needed here, for me it looks like its not even necessary..

<?php
function fib($n)
{
    $cur = 1;
    $prev = 0;
    for ($i = 0; $i < $n; $i++) {
	yield $cur;
        $temp = $cur;
        $cur = $prev + $cur;
        $prev = $temp;
    }
}

$fibs = fib(9);
foreach ($fibs as $fib) {
    echo " " . $fib;
}

This here is just the same:

<?php
function fib($n)
{
	$res = [];
    $cur = 1;
    $prev = 0;
    for ($i = 0; $i < $n; $i++) {
		$res[] = $cur;
        $temp = $cur;
        $cur = $prev + $cur;
        $prev = $temp;
    }
	return $res;
}

$fibs = fib(9);
foreach ($fibs as $fib) {
    echo " " . $fib;
}

from bettergen.

inxomnyaa avatar inxomnyaa commented on June 28, 2024

Notice: Undefined index: generation.level4.manager in phar://C:/Users/Administrator/Desktop/Server/MinecraftPE/WolvesFortressCSphp7/ClearSky-PocketMine-MP_1.6.2dev.phar/src/pocketmine/scheduler/AsyncTask.php on line 142

from bettergen.

Ad5001 avatar Ad5001 commented on June 28, 2024

I see... will fix this this evening.

from bettergen.

inxomnyaa avatar inxomnyaa commented on June 28, 2024

Oh actually i see now. it simply doesn't save an array to memory

from bettergen.

Ad5001 avatar Ad5001 commented on June 28, 2024

@thebigsmileXD There is an another difference, yield pauses the function execution and execute the code on the foreach (where you can send values back using the $gen->send() function).
Your code should look like this:

<?php
function fib($n) {
    $cur = 1;
    for ($i = 0; $i < $n; $i++) {
        echo " " . $cur;
        $cur += yield $cur;
    }
}

$prev = 0;
foreach ($gen = fib(9) as $fib) {
    echo " Foreach:" . $fib;
    $gen->send($fib);
    $prev = $fib;
}

This would output:

1 Foreach:1 2 2 Foreach:2 4 4 Foreach:4 8 8 Foreach:8 16 16 Foreach:16

from bettergen.

Ad5001 avatar Ad5001 commented on June 28, 2024

Uhh. What branch are you runing?

from bettergen.

inxomnyaa avatar inxomnyaa commented on June 28, 2024

Not sure, i guess.. master?

from bettergen.

Ad5001 avatar Ad5001 commented on June 28, 2024

Well on master, BetterNormal::$options is public. Try updating it maybe?

from bettergen.

Ad5001 avatar Ad5001 commented on June 28, 2024

Do anyone still get this bug or I can close it? I cannot reproduce it.

from bettergen.

AvgZing avatar AvgZing commented on June 28, 2024

👍

from bettergen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.