Skip to content

Commit

Permalink
Add constant function (#72)
Browse files Browse the repository at this point in the history
* 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
letnando authored and rgomezcasas committed Apr 11, 2019
1 parent 46fe90f commit 618bb7a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [butlast](functions/butlast.md): Returns all the elements of a collection except the last preserving the keys
* [complement](functions/complement.md): Returns another function that takes the same arguments and has the opposite truth value.
* [compose](functions/compose.md): Combine multiple function calls in one function
* [constant](functions/constant.md): It wraps a value into a Closure, which return the same value whenever is called
* [dissoc](functions/dissoc.md): Dissociate a value of a key in a collection
* [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
* [each](functions/each.md): Apply a function over all the items of a collection
Expand Down
29 changes: 29 additions & 0 deletions docs/functions/constant.md
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]
```
1 change: 1 addition & 0 deletions src/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require __DIR__ . '/butlast.php';
require __DIR__ . '/complement.php';
require __DIR__ . '/compose.php';
require __DIR__ . '/constant.php';
require __DIR__ . '/dissoc.php';
require __DIR__ . '/do_if.php';
require __DIR__ . '/each.php';
Expand Down
19 changes: 19 additions & 0 deletions src/constant.php
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;
};
}
29 changes: 29 additions & 0 deletions tests/ConstantTest.php
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());
}
}

0 comments on commit 618bb7a

Please sign in to comment.