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.In addition to "reduce", this operation is also sometimes called "fold"; in Mathematica:
This second form can be implemented this way:
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:
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).toArraythat would have to be called priorly to passing it toarray_reduce. Not a great idea, either.array_reducethat works with anyTraversableobject. This would be easy to do, but you couldn’t put aTraversabletype hint in the function declaration since arrays are not objects. With such a hint, every array would have to be encapsulated in anArrayIteratorobject prior to the function call.