-
-
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 distinct() dans rsample() methods.
- Loading branch information
Showing
6 changed files
with
125 additions
and
0 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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Contract; | ||
|
||
/** | ||
* Interface Distinctable. | ||
*/ | ||
interface Distinctable | ||
{ | ||
/** | ||
* @return \drupol\collection\Contract\Base | ||
*/ | ||
public function distinct(): Base; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Contract; | ||
|
||
/** | ||
* Interface RSampleable. | ||
*/ | ||
interface RSampleable | ||
{ | ||
/** | ||
* @param float $probability | ||
* | ||
* @return \drupol\collection\Contract\Base | ||
*/ | ||
public function rsample($probability): Base; | ||
} |
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 drupol\collection\Operation; | ||
|
||
use drupol\collection\Collection; | ||
use drupol\collection\Contract\Operation; | ||
|
||
/** | ||
* Class Distinct. | ||
*/ | ||
final class Distinct implements Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection) | ||
{ | ||
return static function () use ($collection) { | ||
$seen = new Collection([]); | ||
|
||
foreach ($collection as $key => $value) { | ||
if (true === $seen->contains($value)) { | ||
continue; | ||
} | ||
|
||
$seen = $seen | ||
->append($value) | ||
->rebase(); | ||
|
||
yield $key => $value; | ||
} | ||
}; | ||
} | ||
} |