Skip to content

Commit

Permalink
Merge pull request #17 from ebanx/backport-stream
Browse files Browse the repository at this point in the history
Backport stream
  • Loading branch information
killertux authored Feb 2, 2022
2 parents 6179b48 + e4a2214 commit 52ca239
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 33 deletions.
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
"description": "A library to help working with streams of data in PHP",
"type": "library",
"license": "MIT",
"version": "1.2.0",
"version": "1.3.0",
"minimum-stability": "stable",
"autoload": {
"psr-4": {"EBANX\\Stream\\": "src/"}
"psr-4": {
"EBANX\\Stream\\": "src/"
}
},
"autoload-dev": {
"psr-4": {"Test\\EBANX\\Stream\\": "test/"}
"psr-4": {
"Test\\EBANX\\Stream\\": "test/"
}
},
"require": {
"php": "^7.1||^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5"
}
}
}
57 changes: 30 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 107 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ public function collectFirst(callable $callback = null, $default = null) {
}

$collected = $this->take(1)->collect();
if (empty($collected)) {
if (empty($collected) && is_null($default)) {
throw new \InvalidArgumentException('No elements available in this stream.');
}
return array_shift($collected);
return array_shift($collected) ?? $default;
}

/**
Expand Down Expand Up @@ -418,6 +418,20 @@ public function min(callable $compare_function = null) {
});
}

/**
* Get the min value from the stream. If two elements have the same value, the first one found is returned.
* This method consumes the stream.
*
* @param callable $key_function The key function to compare against. Data will be transformed by this function before compared.
* @return mixed
*/
public function minBy(callable $key_function) {
return $this->min(static function ($a, $b) use ($key_function) {
return $key_function($b) <=> $key_function($a);
});
}

/**
* Get the max value from the stream. If two elements have the same value, the first one found is returned.
* This method consumes the stream.
Expand All @@ -432,6 +446,20 @@ public function max(callable $compare_function = null) {
});
}

/**
* Get the max value from the stream. If two elements have the same value, the first one found is returned.
* This method consumes the stream.
*
* @param callable $key_function The key function to compare against. Data will be transformed by this function before compared.
* @return mixed
*/
public function maxBy(callable $key_function) {
return $this->max(static function ($a, $b) use ($key_function) {
return $key_function($a) <=> $key_function($b);
});
}

/**
* Using the given accumulator, it calls the callback passing the accumulator and the value of the
* current element of the stream. The return of the callback is then saved into the accumulator and
Expand Down Expand Up @@ -645,6 +673,83 @@ public function takeWhile(callable $callback): Stream {
return new self($generator($this));
}

/**
* Converts the stream into a php Generator
*
* @return \Generator
*/
public function intoGenerator(): \Generator {
foreach ($this as $value) {
yield $value;
}
}

/**
* Rejects the stream's elements that meet the callback's condition.
*
* @param callable $callback
* @return $this
*/
public function reject(callable $callback): self {
$generator = function (Stream $stream) use ($callback): \Generator {
foreach ($stream as $value) {
if (!$callback($value)) {
yield $value;
}
}
};

return new self($generator($this));
}

/**
* Returns true if all stream's elements meet the callback's condition. Returns false otherwise.
*
* @param callable $callback
* @return bool
*/
public function all(callable $callback): bool {
foreach ($this as $value) {
if (!$callback($value)) {
return false;
}
}

return true;
}

/**
* Returns true if none of the stream's elements meet the callback's condition. Returns false otherwise.
*
* @param callable $callback
* @return bool
*/
public function none(callable $callback): bool {
foreach ($this as $value) {
if ($callback($value)) {
return false;
}
}

return true;
}

/**
* Returns true if at least one element from the stream meets the callback's condition. Returns false otherwise.
*
* @param callable $callback
* @return bool
*/
public function any(callable $callback): bool {
foreach ($this as $value) {
if ($callback($value)) {
return true;
}
}

return false;
}

private static function range($start, $end, $step): self {
$generatorAscending = function () use ($start, $end, $step) {
for ($i = $start; $i <= $end; $i += $step) {
Expand Down
Loading

0 comments on commit 52ca239

Please sign in to comment.