Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ary function which ignores arguments over the supplied number #215

Merged
merged 4 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"autoload": {
"psr-4": {"Functional\\": "src/Functional"},
"files": [
"src/Functional/Ary.php",
"src/Functional/Average.php",
"src/Functional/ButLast.php",
"src/Functional/Capture.php",
Expand Down
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
31 changes: 31 additions & 0 deletions src/Functional/Ary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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;

use Functional\Exceptions\InvalidArgumentException;
use Traversable;

/**
* Call $func with only abs($count) arguments, taken either from the
* left or right depending on the sign
*/
function ary(callable $func, int $count): callable
{
InvalidArgumentException::assertNonZeroInteger($count, __FUNCTION__);

return function (...$args) use ($func, $count) {
if ($count > 0) {
return $func(...take_left($args, $count));
} else if ($count < 0) {
return $func(...take_right($args, -$count));
}
};
}
7 changes: 7 additions & 0 deletions src/Functional/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ private static function assertCollectionAlike($collection, $className, $callee,
}
}

public static function assertNonZeroInteger($value, $callee)
{
if (!\is_int($value) || $value == 0) {
throw new static(\sprintf('%s expected parameter %d to be non-zero', $callee, $value));
}
}

private static function getType($value)
{
return \is_object($value) ? \get_class($value) : \gettype($value);
Expand Down
6 changes: 6 additions & 0 deletions src/Functional/Functional.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

final class Functional
{

/**
* @see \Function\ary
*/
const ary = '\Functional\ary';

/**
* @see \Functional\average
*/
Expand Down
41 changes: 41 additions & 0 deletions tests/Functional/AryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;
use Functional\Exceptions\InvalidArgumentException;

class AryTest extends AbstractTestCase
{

public function test()
{
$f = function ($a = 0, $b = 0, $c = 0) {
return $a + $b + $c;
};

$this->assertSame(5, $f(5));
$this->assertSame(5, ary($f, 1)(5));
$this->assertSame(5, ary($f, 1)(5));
$this->assertSame(6, ary($f, -1)(6));
$this->assertSame(7, ary($f, 2)(5, 2));
}

public function testException()
{
$this->expectException(InvalidArgumentException::class);
$f = function ($a = 0, $b = 0, $c = 0) {
return $a + $b + $c;
};

ary($f, 0)(5);
}
}