13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP でカリー化

Last updated at Posted at 2014-10-09

2014-10-10 23:50 追記

GitHub および Packagist に公開しました。
https://github.com/yuya-takeyama/curry
https://packagist.org/packages/yuyat/curry


眠いのでコードのメモだけ。

<?php
class CurryingFunction
{
    private $fn;

    private $count;

    private $args;

    public function __construct($fn, $count, $args)
    {
        $this->fn = $fn;
        $this->count = $count;
        $this->args = $args;
    }

    public function apply($x)
    {
        $count = $this->count;
        $nextArgs = $this->args;

        foreach (func_get_args() as $arg) {
            $nextArgs[] = $arg;
            $count -= 1;

            if ($count < 1) {
                break;
            }
        }

        if ($count < 1) {
            return call_user_func_array($this->fn, $nextArgs);
        } else {
            return new static($this->fn, $count, $nextArgs);
        }
    }

    public function __invoke($x)
    {
        return call_user_func_array(array($this, 'apply'), func_get_args());
    }
}

function curry($fn, $count = null) {
    if (!$count) {
        $ref = new ReflectionFunction($fn);
        $count = $ref->getNumberOfParameters();
    }

    return new CurryingFunction($fn, $count, array());
}

$f = curry(function ($x, $y, $z) { return $x + $y + $z; });

var_dump($f(1)->apply(2)->apply(3));
// => 6
var_dump($f(1, 2)->apply(3));
// => 6
var_dump($f(1, 2, 3));
// => 6
13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?