-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FoldLeft and FoldRight transformations.
- Loading branch information
Showing
7 changed files
with
211 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract; | ||
|
||
/** | ||
* Interface FoldLeftable. | ||
*/ | ||
interface FoldLeftable | ||
{ | ||
/** | ||
* Fold the collection from the left to the right. | ||
* | ||
* @param callable $callback | ||
* @param mixed $initial | ||
* | ||
* @return mixed | ||
*/ | ||
public function foldLeft(callable $callback, $initial = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract; | ||
|
||
/** | ||
* Interface FoldRightable. | ||
*/ | ||
interface FoldRightable | ||
{ | ||
/** | ||
* Fold the collection from the right to the left. | ||
* | ||
* @param callable $callback | ||
* @param mixed $initial | ||
* | ||
* @return mixed | ||
*/ | ||
public function foldRight(callable $callback, $initial = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use loophp\collection\Contract\Transformation; | ||
|
||
/** | ||
* Class FoldLeft. | ||
*/ | ||
final class FoldLeft implements Transformation | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $callback; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $initial; | ||
|
||
/** | ||
* FoldLeft constructor. | ||
* | ||
* @param callable $callback | ||
* @param mixed|null $initial | ||
*/ | ||
public function __construct(callable $callback, $initial = null) | ||
{ | ||
$this->callback = $callback; | ||
$this->initial = $initial; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection) | ||
{ | ||
$callback = $this->callback; | ||
$initial = $this->initial; | ||
|
||
foreach ($collection as $key => $value) { | ||
$initial = $callback($initial, $value, $key); | ||
} | ||
|
||
return $initial; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use loophp\collection\Contract\Transformation; | ||
|
||
/** | ||
* Class FoldRight. | ||
*/ | ||
final class FoldRight implements Transformation | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $callback; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
private $initial; | ||
|
||
/** | ||
* FoldRight constructor. | ||
* | ||
* @param callable $callback | ||
* @param mixed|null $initial | ||
*/ | ||
public function __construct(callable $callback, $initial = null) | ||
{ | ||
$this->callback = $callback; | ||
$this->initial = $initial; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection) | ||
{ | ||
$callback = $this->callback; | ||
$initial = $this->initial; | ||
|
||
foreach ((new Reverse())->on($collection)() as $key => $value) { | ||
$initial = $callback($initial, $value, $key); | ||
} | ||
|
||
return $initial; | ||
} | ||
} |