Skip to content

Commit

Permalink
Add ignore_args which ignores arguments over the supplied number
Browse files Browse the repository at this point in the history
Added function which defines a function of an arbitry number of arguments and
calls the provided callable with only the given number of arguments
  • Loading branch information
someonewithpc committed Mar 28, 2020
1 parent a736d25 commit a019437
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Functional/Functional.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ final class Functional
*/
const if_else = '\Functional\if_else';

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

/**
* @see \Functional\indexes_of
*/
Expand Down
28 changes: 28 additions & 0 deletions src/Functional/IgnoreArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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
*/
public static function ingore_args(callable $func, int $count): callable
{
return function (...$args) use ($func, $count) {
if ($count > 0) {
return call_user_func_array($func, take_left($args, $count));
}
return call_user_func_array($func, take_right($args, -$count));
};
}

0 comments on commit a019437

Please sign in to comment.