We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
$numbers = Pipeline\fromValues(1, 2, 3, 4, 5); $runningTotal = $numbers->scan(function ($accumulator, $value) { return $accumulator + $value; }, 0); foreach ($runningTotal as $total) { echo $total . ', '; } // Output: 0, 1, 3, 6, 10, 15,
Possible implementation:
/** * Performs a scan operation on the pipeline. * * @param callable $func Function that takes the accumulator and an element and returns the new accumulator. * @param mixed $initial Initial value for the accumulator. * @return self */ public function scan(callable $func, $initial = null): self { if (null === $initial) { $this->pipeline = self::makeNonRewindable($this->pipeline); $initial = $this->pipeline->current(); $this->pipeline->next(); } $this->pipeline = self::performScan($this->pipeline, $func, $initial); return $this; } private static function performScan(Generator $input, callable $func, $accumulator): Generator { yield $accumulator; foreach ($input as $value) { $accumulator = $func($accumulator, $value); yield $accumulator; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Possible implementation:
The text was updated successfully, but these errors were encountered: