-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <5954976+fnandot@users.noreply.github.com> * Update docs/functions/constant.md Co-Authored-By: fnandot <5954976+fnandot@users.noreply.github.com>
- Loading branch information
1 parent
46fe90f
commit 618bb7a
Showing
5 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# constant | ||
|
||
## Description | ||
|
||
It wraps a value into a closure, which return the same value that is passed as argument whenever is called. | ||
|
||
## Parameters | ||
|
||
<dl> | ||
<dt>value</dt> | ||
<dd>Any type of value that will be returned</dd> | ||
</dl> | ||
|
||
## Examples | ||
|
||
Fill an existing array with zeros: | ||
|
||
```php | ||
<?php | ||
|
||
use function Lambdish\Phunctional\map; | ||
use function Lambdish\Phunctional\constant; | ||
|
||
$numbers = [1, 2, 3, 4, 5]; | ||
|
||
map(constant(0), $numbers); | ||
|
||
// => [0, 0, 0, 0, 0] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Lambdish\Phunctional; | ||
|
||
/** | ||
* It wraps a value into a Closure, which return the same value whenever is called | ||
* | ||
* @since v1.0.7 | ||
* | ||
* @param mixed $value any type of value | ||
* | ||
* @return \Closure | ||
*/ | ||
function constant($value) | ||
{ | ||
return function () use ($value) { | ||
return $value; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Lambdish\Phunctional\Tests; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use function Lambdish\Phunctional\constant; | ||
|
||
final class ConstantTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @test */ | ||
public function it_should_return_a_value() | ||
{ | ||
$value = 5; | ||
$constantClosure = constant($value); | ||
|
||
$this->assertSame($value, $constantClosure()); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_return_same_value_if_called_twice() | ||
{ | ||
$value = 5; | ||
$constantClosure = constant($value); | ||
|
||
$constantClosure(); | ||
|
||
$this->assertSame($value, $constantClosure()); | ||
} | ||
} |