Skip to content

Commit

Permalink
Add uniq
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbiller committed Feb 16, 2017
1 parent 3a4a871 commit a7275c1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You can also check code coverage by running ```composer run test:coverage```.
- some
- first
- last
- uniq
- Function composition
- flow
- compose
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"src/reject.php",
"src/reverse.php",
"src/some.php",
"src/uniq.php",
"src/zip.php"
]
},
Expand Down
10 changes: 5 additions & 5 deletions composer.lock

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

26 changes: 26 additions & 0 deletions src/uniq.php
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);
}
24 changes: 24 additions & 0 deletions tests/uniq.php
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]);
});
});

0 comments on commit a7275c1

Please sign in to comment.