From e495a97e90d04e44da32b147ec223970370a567a Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 29 Mar 2020 05:31:20 -0500 Subject: [PATCH] Make default callback the identity function. (#214) * Make default callback the identity function. This commit changes the `select`, `reject`, `some`, `none`, and `every` functions so that the default callback argument is the `id` function. * Provide more clarity in docs when $callback is id(). --- docs/functional-php.md | 15 +++++++++++---- src/Functional/Every.php | 8 ++++++-- src/Functional/None.php | 8 ++++++-- src/Functional/Reject.php | 8 ++++++-- src/Functional/Select.php | 8 ++++++-- src/Functional/Some.php | 8 ++++++-- tests/Functional/EveryTest.php | 8 ++++++++ tests/Functional/NoneTest.php | 8 ++++++++ tests/Functional/RejectTest.php | 9 +++++++++ tests/Functional/SelectTest.php | 9 +++++++++ tests/Functional/SomeTest.php | 8 ++++++++ 11 files changed, 83 insertions(+), 14 deletions(-) diff --git a/docs/functional-php.md b/docs/functional-php.md index 81ed3892..7e677a45 100644 --- a/docs/functional-php.md +++ b/docs/functional-php.md @@ -94,7 +94,7 @@ map(range(0, 100), function($v) {return $v + 1;}); ## every() & invoke() -``Functional\every(array|Traversable $collection, callable $callback)`` +``Functional\every(array|Traversable $collection, callable $callback = null)`` ```php is } ``` +If `$callback` is not provided then the `id()` function is used and `every` will return true if every value in the collection is truthy. ## some() -``bool Functional\some(array|Traversable $collection, callable $callback)`` +``bool Functional\some(array|Traversable $collection, callable $callback = null)`` ```php isA } ``` +If `$callback` is not provided then the `id()` function is used and `none` will return true if every value in the collection is falsey. ## reject() & select() -``array Functional\select(array|Traversable $collection, callable $callback)`` +``array Functional\select(array|Traversable $collection, callable $callback = null)`` -``array Functional\reject(array|Traversable $collection, callable $callback)`` +``array Functional\reject(array|Traversable $collection, callable $callback = null)`` ```php $element) { if (!$callback($element, $index, $collection)) { return false; diff --git a/src/Functional/None.php b/src/Functional/None.php index 693d9c1d..34704737 100644 --- a/src/Functional/None.php +++ b/src/Functional/None.php @@ -18,13 +18,17 @@ * Callback arguments will be element, index, collection. * * @param Traversable|array $collection - * @param callable $callback + * @param callable|null $callback * @return bool */ -function none($collection, callable $callback) +function none($collection, callable $callback = null) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); + if ($callback === null) { + $callback = '\Functional\id'; + } + foreach ($collection as $index => $element) { if ($callback($element, $index, $collection)) { return false; diff --git a/src/Functional/Reject.php b/src/Functional/Reject.php index 5665f895..1a3bbe40 100644 --- a/src/Functional/Reject.php +++ b/src/Functional/Reject.php @@ -18,15 +18,19 @@ * Functional\select(). Callback arguments will be element, index, collection * * @param Traversable|array $collection - * @param callable $callback + * @param callable|null $callback * @return array */ -function reject($collection, callable $callback) +function reject($collection, callable $callback = null) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); $aggregation = []; + if ($callback === null) { + $callback = '\Functional\id'; + } + foreach ($collection as $index => $element) { if (!$callback($element, $index, $collection)) { $aggregation[$index] = $element; diff --git a/src/Functional/Select.php b/src/Functional/Select.php index de0ea701..1e6b59ec 100644 --- a/src/Functional/Select.php +++ b/src/Functional/Select.php @@ -18,15 +18,19 @@ * Opposite is Functional\reject(). Callback arguments will be element, index, collection * * @param Traversable|array $collection - * @param callable $callback + * @param callable|null $callback * @return array */ -function select($collection, callable $callback) +function select($collection, callable $callback = null) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); $aggregation = []; + if ($callback === null) { + $callback = '\Functional\id'; + } + foreach ($collection as $index => $element) { if ($callback($element, $index, $collection)) { $aggregation[$index] = $element; diff --git a/src/Functional/Some.php b/src/Functional/Some.php index f90d5aba..5583dac4 100644 --- a/src/Functional/Some.php +++ b/src/Functional/Some.php @@ -18,13 +18,17 @@ * traversing the collection if a truthy element is found. Callback arguments will be value, index, collection * * @param Traversable|array $collection - * @param callable $callback + * @param callable|null $callback * @return bool */ -function some($collection, callable $callback) +function some($collection, callable $callback = null) { InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1); + if ($callback === null) { + $callback = '\Functional\id'; + } + foreach ($collection as $index => $element) { if ($callback($element, $index, $collection)) { return true; diff --git a/tests/Functional/EveryTest.php b/tests/Functional/EveryTest.php index a77143b8..25ad1f64 100644 --- a/tests/Functional/EveryTest.php +++ b/tests/Functional/EveryTest.php @@ -46,6 +46,14 @@ public function testPassNoCollection() every('invalidCollection', 'strlen'); } + public function testPassNoCallable() + { + $this->assertTrue(every($this->goodArray)); + $this->assertTrue(every($this->goodIterator)); + $this->assertTrue(every($this->badArray)); + $this->assertTrue(every($this->badIterator)); + } + public function testExceptionIsThrownInArray() { $this->expectException('DomainException'); diff --git a/tests/Functional/NoneTest.php b/tests/Functional/NoneTest.php index cbcf5feb..f13bef53 100644 --- a/tests/Functional/NoneTest.php +++ b/tests/Functional/NoneTest.php @@ -46,6 +46,14 @@ public function testPassNonCallable() none($this->goodArray, 'undefinedFunction'); } + public function testPassNoCallable() + { + $this->assertFalse(none($this->goodArray)); + $this->assertFalse(none($this->goodIterator)); + $this->assertFalse(none($this->badArray)); + $this->assertFalse(none($this->badIterator)); + } + public function testExceptionIsThrownInArray() { $this->expectException('DomainException'); diff --git a/tests/Functional/RejectTest.php b/tests/Functional/RejectTest.php index 0b9e1e26..6ff2052f 100644 --- a/tests/Functional/RejectTest.php +++ b/tests/Functional/RejectTest.php @@ -44,6 +44,15 @@ public function testPassNonCallable() reject($this->list, 'undefinedFunction'); } + public function testPassNoCallable() + { + $this->assertSame([], reject($this->list)); + $this->assertSame([], reject($this->listIterator)); + $this->assertSame([], reject($this->hash)); + $this->assertSame([], reject($this->hashIterator)); + $this->assertSame([1 => false], reject([true, false, true])); + } + public function testPassNoCollection() { $this->expectArgumentError('Functional\reject() expects parameter 1 to be array or instance of Traversable'); diff --git a/tests/Functional/SelectTest.php b/tests/Functional/SelectTest.php index 0b6cc53b..561e9a92 100644 --- a/tests/Functional/SelectTest.php +++ b/tests/Functional/SelectTest.php @@ -64,6 +64,15 @@ public function testPassNonCallable($functionName) $functionName($this->list, 'undefinedFunction'); } + public function testPassNoCallable() + { + $this->assertSame(['value', 'wrong', 'value'], select($this->list)); + $this->assertSame(['value', 'wrong', 'value'], select($this->listIterator)); + $this->assertSame(['k1' => 'value', 'k2' => 'wrong', 'k3' => 'value'], select($this->hash)); + $this->assertSame(['k1' => 'value', 'k2' => 'wrong', 'k3' => 'value'], select($this->hashIterator)); + $this->assertSame([0 => true, 2 => true], select([true, false, true])); + } + /** * @dataProvider getAliases */ diff --git a/tests/Functional/SomeTest.php b/tests/Functional/SomeTest.php index 9213f7dc..2090118f 100644 --- a/tests/Functional/SomeTest.php +++ b/tests/Functional/SomeTest.php @@ -40,6 +40,14 @@ public function testPassNonCallable() some($this->goodArray, 'undefinedFunction'); } + public function testPassNoCallable() + { + $this->assertTrue(some($this->goodArray)); + $this->assertTrue(some($this->goodIterator)); + $this->assertTrue(some($this->badArray)); + $this->assertTrue(some($this->badIterator)); + } + public function testPassNoCollection() { $this->expectArgumentError('Functional\some() expects parameter 1 to be array or instance of Traversable');