Skip to content

Fix generator xrange() example #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

KodyWiremane
Copy link

First, the increment part in the else branch's loop must be +=, not -=, because $step is negative there.
Second, $start == $limit is OK for range(), the resulting sequence will simply contain only one value.

Also, I've noticed some deviation from how range() behaves: it ignores $step's sign, and derives it from which of $start and $limit is greater. It also emits a warning if $start != $limit && abs($step) > abs($limit - $start) , and returns false (as well as if $step == 0).

If xrange() is thought of as an equivalent of range(), this should be addressed. Though, personally, I more like xrange() the way it is (except for the two errors initially mentioned), as it looks more consistent.

To check for [dis]similarities between range() and xrange(), I've used

the following code
define('MAX_ITERATIONS', 20);

function testFunction($func, $args) {
    list($from, $to, $step) = $args;
    $counter = 0;
    try {
        foreach ($func($from, $to, $step) as $number) {
            echo " $number";
            $counter++;
            if ($counter > MAX_ITERATIONS) {
                echo ' INT' . PHP_EOL;
                break;
            }
        }
    } catch (Throwable $e) {
        echo " EXP: {$e->getMessage()}";
    }
    echo PHP_EOL;
}

$cases = [
    [2, 11, 2],
    [2, 11, -2],
    [2, 11, 0],
    [2, 2, 0],
    [11, 2, 2],
    [11, 2, -2],
    [2, 2, 2],
    [2, -2, -2],
    [2, 4, 10]
];

foreach ($cases as $case) {

    $caseAsString = implode(', ', $case);

    echo "Testing [$caseAsString]:" . PHP_EOL;
    echo 'range(): ';
    testFunction('range', $case);
    echo 'xrange():';
    testFunction('xrange', $case);
    echo PHP_EOL;

}

@nikic
Copy link
Member

nikic commented Jul 13, 2019

Thanks! I don't think it's necessary to match range() exactly, as long as the code is correct and illustrates the generator usage.

@php-pulls merge

@php-pulls php-pulls closed this in bf8fc47 Jul 13, 2019
salathe pushed a commit to salathe/phpdoc-en that referenced this pull request Jul 13, 2019
Patch by KodyWiremane.

Closes php/doc-en#4.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@347735 c90b9560-bf6c-de11-be94-00142212c4b1
heiglandreas pushed a commit to phpdoctest/en that referenced this pull request Feb 4, 2020
Patch by KodyWiremane.

Closes php/doc-en#4.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@347735 c90b9560-bf6c-de11-be94-00142212c4b1
salathe pushed a commit to salathe/phpdoc-en that referenced this pull request Sep 3, 2020
Patch by KodyWiremane.

Closes php/doc-en#4.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@347735 c90b9560-bf6c-de11-be94-00142212c4b1
Firehed added a commit to Firehed/doc-en that referenced this pull request Mar 18, 2021
The documented parameters differ from the names exposed in reflection/source code. Now that named parameters are supported in PHP 8, this discrepancy is confusing and impactful.

```
 $ php --rf fgetcsv
Function [ <internal:standard> function fgetcsv ] {

  - Parameters [5] {
    Parameter #0 [ <required> $stream ]
    Parameter php#1 [ <optional> ?int $length = null ]
    Parameter php#2 [ <optional> string $separator = "," ]
    Parameter php#3 [ <optional> string $enclosure = "\"" ]
    Parameter php#4 [ <optional> string $escape = "\\" ]
  }
  - Return [ array|false ]
}
```
php-pulls pushed a commit that referenced this pull request Mar 18, 2021
The documented parameters differ from the names exposed in reflection/source code. Now that named parameters are supported in PHP 8, this discrepancy is confusing and impactful.

```
 $ php --rf fgetcsv
Function [ <internal:standard> function fgetcsv ] {

  - Parameters [5] {
    Parameter #0 [ <required> $stream ]
    Parameter #1 [ <optional> ?int $length = null ]
    Parameter #2 [ <optional> string $separator = "," ]
    Parameter #3 [ <optional> string $enclosure = "\"" ]
    Parameter #4 [ <optional> string $escape = "\\" ]
  }
  - Return [ array|false ]
}
```

Closes GH-493.
aghArdeshir added a commit to aghArdeshir/doc-en that referenced this pull request Aug 9, 2023
in this example, `$count--` at the end of the function helps with nothing. Its existence also does not harm anything. But because in the same file, in "Example php#4 Example demonstrating need for static variables" we were talking about how useless `$a++` is in that example, putting a useless `$count--` can be misleading in this example, because the reader may look for the philosophy/reason behind existence of this statement.
kamil-tekiela added a commit to kamil-tekiela/doc-en that referenced this pull request Dec 5, 2024
Girgias pushed a commit that referenced this pull request Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants