Skip to content

Commit b5a50d2

Browse files
authored
Add identity function (#80)
* Changed constant function name to identity * Added the behaviour of real identity function * Fixed a typo * Changed identity function description * Changed identity function example * Changed identity function description * Changed identity function description in index * Improve identity tests
1 parent cdd6d53 commit b5a50d2

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

docs/docs.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [get_in](functions/get_in.md): Returns the value in a nested associative structure or a default value in the case it does not exists
2323
* [group_by](functions/group_by.md): Returns an array with the items grouped by the results of applying a function to each item
2424
* [key](functions/key.md): Returns the key of an item value in a collection or a default value in the case it does not exists
25+
* [identity](functions/identity.md): Returns the same value that is passed as argument
2526
* [instance_of](functions/instance_of.md): Returns a checker that validated if an element is an instance of a class
2627
* [last](functions/last.md): Returns the last element of a collection
2728
* [map](functions/map.md): Apply a function over all the items of a collection and returns an array with the results

docs/functions/identity.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# identity
2+
3+
## Description
4+
5+
Identity function is a function which return the same value that is passed as argument. `f(x) = x`
6+
7+
## Parameters
8+
9+
<dl>
10+
<dt>value</dt>
11+
<dd>Any type of value that will be returned</dd>
12+
</dl>
13+
14+
## Examples
15+
16+
Do you want to check if an array of booleans has a at least a true value:
17+
18+
```php
19+
<?php
20+
21+
use function Lambdish\Phunctional\any;
22+
23+
$values = [false, false, true, false];
24+
25+
any('Lambdish\Phunctional\identity', $values);
26+
27+
// => true
28+
```

src/_bootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require __DIR__ . '/get_in.php';
2121
require __DIR__ . '/group_by.php';
2222
require __DIR__ . '/key.php';
23+
require __DIR__ . '/identity.php';
2324
require __DIR__ . '/instance_of.php';
2425
require __DIR__ . '/last.php';
2526
require __DIR__ . '/map.php';

src/identity.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Lambdish\Phunctional;
4+
5+
/**
6+
* Identity function is a function which return the same value that is passed as argument. `f(x) = x`
7+
*
8+
* @since v1.0.9
9+
*
10+
* @param mixed $argument any type of value
11+
*
12+
* @return mixed
13+
*/
14+
function identity($argument)
15+
{
16+
return $argument;
17+
}

tests/IdentityTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Lambdish\Phunctional\Tests;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use stdClass;
7+
use function Lambdish\Phunctional\identity;
8+
9+
final class IdentityTest extends PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @test
13+
* @dataProvider values()
14+
*/
15+
public function it_should_return_a_same_value_is_passed_as_argument($value)
16+
{
17+
$this->assertSame($value, identity($value));
18+
}
19+
20+
public function values()
21+
{
22+
return [
23+
[1],
24+
[9],
25+
[0],
26+
['a'],
27+
['b'],
28+
['some text'],
29+
[new stdClass()],
30+
[[]],
31+
[['test', 'array']],
32+
[null],
33+
];
34+
}
35+
}

0 commit comments

Comments
 (0)