Skip to content

Commit

Permalink
Added test for ary function
Browse files Browse the repository at this point in the history
  • Loading branch information
someonewithpc committed May 25, 2020
1 parent 65f5e9e commit c199d40
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/functional-php.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ $partiallyAppliedSubtractor = partial_right($subtractor, 10);
$partiallyAppliedSubtractor(20); // -> 10
```

## ary()

`Functional\ary` (as in arity) takes a `callable` and a count and calls the `callable` with
that many arguments using `take_left` if positive, or `take_right` if negative.
Throws if passed 0.

For example:

```php
use function Functional\ary;
use function Functional\map;

# This fails because map calls it's callable with the element,
# index and the whole collection
# map($array, 'ucfirst');

# Using `ary`
map($array, ary('ucfirst', 1')); # Passes only the first argument to `ucfirst`
```

## partial_any()

There is a third function in the family called `partial_any`. Unlike its siblings it doesn’t automatically merge but it
Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/AryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* @package Functional-php
* @author Hugo Sales <hugo@fc.up.pt>
* @copyright 2020 Hugo Sales
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/

namespace Functional\Tests;

use function Functional\ary;

class AryTest extends AbstractTestCase
{

public function test()
{
$this->assertSame(5, ary(function ($a = 0, $b = 0) { return $a + $b; }, 1)(5));
$this->assertSame(5, ary(function ($a = 0, $b = 0, $c = 0) { return $a + $b + $c; }, 1)(5));
$this->assertSame(6, ary(function ($a = 0, $b = 0, $c = 0) { return $a + $b + $c; }, -1)(6));
$this->assertSame(7, ary(function ($a = 0, $b = 0, $c = 0) { return $a + $b + $c; }, 2)(5, 2));
$this->assertNull(ary(function ($a = 0, $b = 0) { return $a + $b; }, 0)(5));
}
}

0 comments on commit c199d40

Please sign in to comment.