tags:

views:

80

answers:

1

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; } 
    ); 
+4  A: 

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.
Artefacto
Exactly what I'd been looking for. Thanks.
quantumSoup
And calling back to your earlier question, `$callback` is where that anonymous function lives.
Charles
Not sure exactly how `array_reduce` function here works, but `inject` in Ruby is a method of `Enumerable` module, meaning that it's enough for a class to implement `each` method, for methods like `map` and `reduce` to work out-of-the box. Of course, they already work for internal classes that implement Enumerable, such as Arrays, Hashes, Ranges etc.
Mladen Jablanović
@Mladen I edited my response to reply to you, since there's not enough space here.
Artefacto
Hey, thanks for the response!
Mladen Jablanović