Skip to content

Commit

Permalink
Changed constant function name to identity
Browse files Browse the repository at this point in the history
  • Loading branch information
letnando committed Mar 20, 2019
1 parent 6173c2b commit e2bfdb2
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* [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 All @@ -20,6 +19,7 @@
* [get](functions/get.md): Returns the value of an item in a collection or a default value in the case it does not exists
* [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
* [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
* [identity](functions/identity.md): Returns a closure which always return the same value that is passed as argument
* [instance_of](functions/instance_of.md): Returns a checker that validated if an element is an instance of a class
* [last](functions/last.md): Returns the last element of a collection
* [map](functions/map.md): Apply a function over all the items of a collection and returns an array with the results
Expand Down
29 changes: 0 additions & 29 deletions docs/functions/constant.md

This file was deleted.

29 changes: 29 additions & 0 deletions docs/functions/identity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# identity

## 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 array with zeros:

```php
<?php

use function Lambdish\Phunctional\map;
use function Lambdish\Phunctional\identity;

$numbers = [1,2,3,4,5];

map(identity(0), $numbers);

// => [0,0,0,0,0]
```
2 changes: 1 addition & 1 deletion src/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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 All @@ -18,6 +17,7 @@
require __DIR__ . '/get.php';
require __DIR__ . '/get_in.php';
require __DIR__ . '/key.php';
require __DIR__ . '/identity.php';
require __DIR__ . '/instance_of.php';
require __DIR__ . '/last.php';
require __DIR__ . '/map.php';
Expand Down
4 changes: 2 additions & 2 deletions src/constant.php → src/identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Lambdish\Phunctional;

/**
* It wraps a value into a Closure, which return the same value whenever is called
* It wraps a value into a Closure, which return the same value that is passed as argument whenever is called
*
* @since v1.0.7
*
* @param mixed $value any type of value
*
* @return \Closure
*/
function constant($value)
function identity($value)
{
return function () use ($value) {
return $value;
Expand Down
29 changes: 0 additions & 29 deletions tests/ConstantTest.php

This file was deleted.

29 changes: 29 additions & 0 deletions tests/IdentityTest.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\identity;

final class IdentityTest extends PHPUnit_Framework_TestCase
{
/** @test */
public function it_should_return_a_value()
{
$value = 5;
$identityClosure = identity($value);

$this->assertSame($value, $identityClosure());
}

/** @test */
public function it_should_return_same_value_if_called_twice()
{
$value = 5;
$identityClosure = identity($value);

$identityClosure();

$this->assertSame($value, $identityClosure());
}
}

0 comments on commit e2bfdb2

Please sign in to comment.