Skip to content

Commit 618bb7a

Browse files
letnandorgomezcasas
authored andcommitted
Add constant function (#72)
* Added constant function * Changed constant function description * Fixed a typo in tests * Changed constant function name to identity * Revert "Changed constant function name to identity" This reverts commit e2bfdb2. * Fixed wrong description on constant function documentation * Some coding style fixes * Update docs/functions/constant.md Co-Authored-By: fnandot <[email protected]> * Update docs/functions/constant.md Co-Authored-By: fnandot <[email protected]>
1 parent 46fe90f commit 618bb7a

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

docs/docs.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [butlast](functions/butlast.md): Returns all the elements of a collection except the last preserving the keys
1010
* [complement](functions/complement.md): Returns another function that takes the same arguments and has the opposite truth value.
1111
* [compose](functions/compose.md): Combine multiple function calls in one function
12+
* [constant](functions/constant.md): It wraps a value into a Closure, which return the same value whenever is called
1213
* [dissoc](functions/dissoc.md): Dissociate a value of a key in a collection
1314
* [do_if](functions/do_if.md): Returns a callable that will call the given function if the result of applying the callable arguments to the predicates is true for all of them
1415
* [each](functions/each.md): Apply a function over all the items of a collection

docs/functions/constant.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# constant
2+
3+
## Description
4+
5+
It wraps a value into a closure, which return the same value that is passed as argument whenever is called.
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+
Fill an existing array with zeros:
17+
18+
```php
19+
<?php
20+
21+
use function Lambdish\Phunctional\map;
22+
use function Lambdish\Phunctional\constant;
23+
24+
$numbers = [1, 2, 3, 4, 5];
25+
26+
map(constant(0), $numbers);
27+
28+
// => [0, 0, 0, 0, 0]
29+
```

src/_bootstrap.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require __DIR__ . '/butlast.php';
88
require __DIR__ . '/complement.php';
99
require __DIR__ . '/compose.php';
10+
require __DIR__ . '/constant.php';
1011
require __DIR__ . '/dissoc.php';
1112
require __DIR__ . '/do_if.php';
1213
require __DIR__ . '/each.php';

src/constant.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Lambdish\Phunctional;
4+
5+
/**
6+
* It wraps a value into a Closure, which return the same value whenever is called
7+
*
8+
* @since v1.0.7
9+
*
10+
* @param mixed $value any type of value
11+
*
12+
* @return \Closure
13+
*/
14+
function constant($value)
15+
{
16+
return function () use ($value) {
17+
return $value;
18+
};
19+
}

tests/ConstantTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Lambdish\Phunctional\Tests;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use function Lambdish\Phunctional\constant;
7+
8+
final class ConstantTest extends PHPUnit_Framework_TestCase
9+
{
10+
/** @test */
11+
public function it_should_return_a_value()
12+
{
13+
$value = 5;
14+
$constantClosure = constant($value);
15+
16+
$this->assertSame($value, $constantClosure());
17+
}
18+
19+
/** @test */
20+
public function it_should_return_same_value_if_called_twice()
21+
{
22+
$value = 5;
23+
$constantClosure = constant($value);
24+
25+
$constantClosure();
26+
27+
$this->assertSame($value, $constantClosure());
28+
}
29+
}

0 commit comments

Comments
 (0)