-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
"src/reject.php", | ||
"src/reverse.php", | ||
"src/some.php", | ||
"src/uniq.php", | ||
"src/zip.php" | ||
] | ||
}, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,26 @@ | ||
<?php | ||
|
||
namespace fphp; | ||
|
||
/** | ||
* Return an array without duplicates | ||
* | ||
* @param array $collection collection | ||
* @return array | ||
* @author Rémy Peru <[email protected]> | ||
*/ | ||
function uniq(...$args) { | ||
$uniq = function (array $collection) { | ||
$uniqArray = []; | ||
|
||
foreach($collection as $item) { | ||
if (!includes($item, $uniqArray)) { | ||
$uniqArray[] = $item; | ||
} | ||
} | ||
|
||
return $uniqArray; | ||
}; | ||
|
||
return curryN($uniq, 1)(...$args); | ||
} |
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,24 @@ | ||
<?php | ||
|
||
use function fphp\uniq; | ||
|
||
describe('uniq', function () { | ||
it('should return input array without duplicates', function () { | ||
$res = uniq([1, 2, 3, 3 ,4 ,5 , 'orange', 'green', 'orange']); | ||
|
||
expect($res)->toBe([1, 2, 3, 4 ,5 , 'orange', 'green']); | ||
}); | ||
|
||
it('should be curried', function () { | ||
$res = uniq(); | ||
$res = $res([1, 2, 3, 3 ,4 ,5 , 'orange', 'green', 'orange']); | ||
|
||
expect($res)->toBe([1, 2, 3, 4 ,5 , 'orange', 'green']); | ||
}); | ||
|
||
it('should work with non contiguous arrays', function () { | ||
$res = uniq([0 => 1, 4 => 2, 8 => 2]); | ||
|
||
expect($res)->toBe([1, 2]); | ||
}); | ||
}); |