-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from InnoT20/support-max-collection
[Collection] Add support max and maxBy to collections
- Loading branch information
Showing
7 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fp\Operations; | ||
|
||
use Fp\Functional\Option\Option; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @template TK | ||
* @template TV | ||
* @extends AbstractOperation<TK, TV> | ||
*/ | ||
class MaxByElementOperation extends AbstractOperation | ||
{ | ||
/** | ||
* @psalm-param callable(TV): mixed $by | ||
* @return Option<TV> | ||
*/ | ||
public function __invoke(callable $by): Option | ||
{ | ||
$f = | ||
/** | ||
* @param TV $r | ||
* @param TV $l | ||
* @return int | ||
*/ | ||
fn(mixed $r, mixed $l): int => $by($l) <=> $by($r); | ||
|
||
$gen = SortedOperation::of($this->gen)($f); | ||
|
||
return FirstOperation::of($gen)(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fp\Operations; | ||
|
||
use Fp\Functional\Option\Option; | ||
|
||
use function Fp\Cast\asList; | ||
|
||
/** | ||
* @psalm-immutable | ||
* @template TK | ||
* @template TV | ||
* @extends AbstractOperation<TK, TV> | ||
*/ | ||
class MaxElementOperation extends AbstractOperation | ||
{ | ||
/** | ||
* @return Option<TV> | ||
*/ | ||
public function __invoke(): Option | ||
{ | ||
$list = asList($this->gen); | ||
|
||
return Option::fromNullable(!empty($list) ? max($list) : null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters