开发者

Emulate ruby's inject() behavior in PHP

From this question here, I was writing an enum wrapper to have some methods that can be used with lambdas to somewhat emulate ruby's usage of blocks in enums.

class enum {
    public $arr;

    function __construct($array) {
        $this->arr = $array;
    }

    function each($lambda) {
        array_walk($this->arr, $lambda);
    }

    function find_all($lambda) {
        return array_filter($this->arr, $lambda);
    }

    function inject($lambda, $initial=null) { 
        if ($initial == null) { 
            $first = array_shift($this->arr); 
            $result = array_reduce($this->arr, $lambda, $first); 
            array_unshift($this->arr, $first); 

            return $result; 
        } else { 
            return array_reduce($this->arr, $lambda, $initial); 
        } 
    } 

}


$list = new enum(array(-1, 3, 4, 5, -7));
$list->each(function($a) { print $a . "\n";});

// in PHP you can also assign a closure to a variable 
$pos = function($a) { return ($a < 0) ? false : true;};
$positives = $list->find_all($pos);

Now, how could I implement inject() as elegantly as possible?


EDIT: method implemented as seen above. Usage examples:

// inject() examples 
$list = new enum(range(5, 10)); 

$sum开发者_运维知识库 = $list->inject(function($sum, $n) { return $sum+$n; }); 
$product = $list->inject(function($acc, $n) { return $acc*$n; }, 1); 

$list = new enum(array('cat', 'sheep', 'bear')); 
$longest = $list->inject(function($memo, $word) { 
        return (strlen($memo) > strlen($word)) ? $memo : $word; } 
    ); 


I'm not familiar with Ruby, but from the description, it seems similar to array_reduce.

mixed array_reduce ( array $input , callback $function [, mixed $initial = NULL ] )

array_reduce() applies iteratively the function function to the elements of the array input, so as to reduce the array to a single value.

In addition to "reduce", this operation is also sometimes called "fold"; in Mathematica:

Fold[f, init, {a, b, c, d}] == f[f[f[f[init, a], b], c], d]

The second form uses the first element of the collection as a the initial value (and skips that element while iterating).

This second form can be implemented this way:

//$arr is the initial array
$first = array_shift($arr);
$result = array_reduce($arr, $callback, $first);

Response to Mladen

The array functions in PHP cannot be used that way because they can only work with arrays, not arbitrary objects.

There are a few options here:

  • You could convert the object into an array prior to passing it to array_reduce. In practice, this doesn't have much value because the conversion consists of creating an array with the object properties as elements. This behavior can only be changed internally (writing a native extension).
  • You could have all your objects implement an interface with a method toArray that would have to be called priorly to passing it to array_reduce. Not a great idea, either.
  • You could implement a version of array_reduce that works with any Traversable object. This would be easy to do, but you couldn't put a Traversable type hint in the function declaration since arrays are not objects. With such a hint, every array would have to be encapsulated in an ArrayIterator object prior to the function call.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜