Skip to content

Commit e495a97

Browse files
authored
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().
1 parent a736d25 commit e495a97

File tree

11 files changed

+83
-14
lines changed

11 files changed

+83
-14
lines changed

docs/functional-php.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ map(range(0, 100), function($v) {return $v + 1;});
9494

9595
## every() & invoke()
9696

97-
``Functional\every(array|Traversable $collection, callable $callback)``
97+
``Functional\every(array|Traversable $collection, callable $callback = null)``
9898

9999
```php
100100
<?php
@@ -107,10 +107,11 @@ if (every($users, function($user, $collectionKey, $collection) {return $user->is
107107
}
108108
```
109109

110+
If `$callback` is not provided then the `id()` function is used and `every` will return true if every value in the collection is truthy.
110111

111112
## some()
112113

113-
``bool Functional\some(array|Traversable $collection, callable $callback)``
114+
``bool Functional\some(array|Traversable $collection, callable $callback = null)``
114115

115116
```php
116117
<?php
@@ -121,6 +122,7 @@ if (some($users, function($user, $collectionKey, $collection) use($me) {return $
121122
}
122123
```
123124

125+
If `$callback` is not provided then the `id()` function is used and `some` will return true if at least one value in the collection is truthy.
124126

125127
## none()
126128

@@ -135,12 +137,13 @@ if (none($users, function($user, $collectionKey, $collection) {return $user->isA
135137
}
136138
```
137139

140+
If `$callback` is not provided then the `id()` function is used and `none` will return true if every value in the collection is falsey.
138141

139142
## reject() & select()
140143

141-
``array Functional\select(array|Traversable $collection, callable $callback)``
144+
``array Functional\select(array|Traversable $collection, callable $callback = null)``
142145

143-
``array Functional\reject(array|Traversable $collection, callable $callback)``
146+
``array Functional\reject(array|Traversable $collection, callable $callback = null)``
144147

145148
```php
146149
<?php
@@ -154,6 +157,10 @@ $activeUsers = select($users, $fn);
154157
$inactiveUsers = reject($users, $fn);
155158
```
156159

160+
For both functions array keys are preserved.
161+
162+
For both functions if a value for $callback is not provided then the `id()` function is used. For `select`, this means that only the truthy values in the collection will be returned. For `reject`, this means that only the falsey values in the collection will be returned.
163+
157164
Alias for `Functional\select()` is `Functional\filter()`
158165

159166
**Note:** This may unexpectedly turn your indexed array into an associative array, [see here](https://github.com/lstrojny/functional-php/issues/39#issuecomment-48034617) if you always want to keep an indexed array.

src/Functional/Every.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
* Callback arguments will be element, index, collection
1919
*
2020
* @param Traversable|array $collection
21-
* @param callable $callback
21+
* @param callable|null $callback
2222
* @return bool
2323
*/
24-
function every($collection, callable $callback)
24+
function every($collection, callable $callback = null)
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

28+
if ($callback === null) {
29+
$callback = '\Functional\id';
30+
}
31+
2832
foreach ($collection as $index => $element) {
2933
if (!$callback($element, $index, $collection)) {
3034
return false;

src/Functional/None.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
* Callback arguments will be element, index, collection.
1919
*
2020
* @param Traversable|array $collection
21-
* @param callable $callback
21+
* @param callable|null $callback
2222
* @return bool
2323
*/
24-
function none($collection, callable $callback)
24+
function none($collection, callable $callback = null)
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

28+
if ($callback === null) {
29+
$callback = '\Functional\id';
30+
}
31+
2832
foreach ($collection as $index => $element) {
2933
if ($callback($element, $index, $collection)) {
3034
return false;

src/Functional/Reject.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
* Functional\select(). Callback arguments will be element, index, collection
1919
*
2020
* @param Traversable|array $collection
21-
* @param callable $callback
21+
* @param callable|null $callback
2222
* @return array
2323
*/
24-
function reject($collection, callable $callback)
24+
function reject($collection, callable $callback = null)
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

2828
$aggregation = [];
2929

30+
if ($callback === null) {
31+
$callback = '\Functional\id';
32+
}
33+
3034
foreach ($collection as $index => $element) {
3135
if (!$callback($element, $index, $collection)) {
3236
$aggregation[$index] = $element;

src/Functional/Select.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
* Opposite is Functional\reject(). Callback arguments will be element, index, collection
1919
*
2020
* @param Traversable|array $collection
21-
* @param callable $callback
21+
* @param callable|null $callback
2222
* @return array
2323
*/
24-
function select($collection, callable $callback)
24+
function select($collection, callable $callback = null)
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

2828
$aggregation = [];
2929

30+
if ($callback === null) {
31+
$callback = '\Functional\id';
32+
}
33+
3034
foreach ($collection as $index => $element) {
3135
if ($callback($element, $index, $collection)) {
3236
$aggregation[$index] = $element;

src/Functional/Some.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
* traversing the collection if a truthy element is found. Callback arguments will be value, index, collection
1919
*
2020
* @param Traversable|array $collection
21-
* @param callable $callback
21+
* @param callable|null $callback
2222
* @return bool
2323
*/
24-
function some($collection, callable $callback)
24+
function some($collection, callable $callback = null)
2525
{
2626
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
2727

28+
if ($callback === null) {
29+
$callback = '\Functional\id';
30+
}
31+
2832
foreach ($collection as $index => $element) {
2933
if ($callback($element, $index, $collection)) {
3034
return true;

tests/Functional/EveryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function testPassNoCollection()
4646
every('invalidCollection', 'strlen');
4747
}
4848

49+
public function testPassNoCallable()
50+
{
51+
$this->assertTrue(every($this->goodArray));
52+
$this->assertTrue(every($this->goodIterator));
53+
$this->assertTrue(every($this->badArray));
54+
$this->assertTrue(every($this->badIterator));
55+
}
56+
4957
public function testExceptionIsThrownInArray()
5058
{
5159
$this->expectException('DomainException');

tests/Functional/NoneTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function testPassNonCallable()
4646
none($this->goodArray, 'undefinedFunction');
4747
}
4848

49+
public function testPassNoCallable()
50+
{
51+
$this->assertFalse(none($this->goodArray));
52+
$this->assertFalse(none($this->goodIterator));
53+
$this->assertFalse(none($this->badArray));
54+
$this->assertFalse(none($this->badIterator));
55+
}
56+
4957
public function testExceptionIsThrownInArray()
5058
{
5159
$this->expectException('DomainException');

tests/Functional/RejectTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public function testPassNonCallable()
4444
reject($this->list, 'undefinedFunction');
4545
}
4646

47+
public function testPassNoCallable()
48+
{
49+
$this->assertSame([], reject($this->list));
50+
$this->assertSame([], reject($this->listIterator));
51+
$this->assertSame([], reject($this->hash));
52+
$this->assertSame([], reject($this->hashIterator));
53+
$this->assertSame([1 => false], reject([true, false, true]));
54+
}
55+
4756
public function testPassNoCollection()
4857
{
4958
$this->expectArgumentError('Functional\reject() expects parameter 1 to be array or instance of Traversable');

tests/Functional/SelectTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public function testPassNonCallable($functionName)
6464
$functionName($this->list, 'undefinedFunction');
6565
}
6666

67+
public function testPassNoCallable()
68+
{
69+
$this->assertSame(['value', 'wrong', 'value'], select($this->list));
70+
$this->assertSame(['value', 'wrong', 'value'], select($this->listIterator));
71+
$this->assertSame(['k1' => 'value', 'k2' => 'wrong', 'k3' => 'value'], select($this->hash));
72+
$this->assertSame(['k1' => 'value', 'k2' => 'wrong', 'k3' => 'value'], select($this->hashIterator));
73+
$this->assertSame([0 => true, 2 => true], select([true, false, true]));
74+
}
75+
6776
/**
6877
* @dataProvider getAliases
6978
*/

0 commit comments

Comments
 (0)