public function testUnderscoreFindsRightClassToCall() { $numbers = array(3, 4, 5); $product = Underscore::reduce($numbers, function ($w, $v) { return $w * $v; }, 1); $this->assertEquals(60, $product); }
function _reduce($list, $iterator, $memo, $context = NULL) { return Underscore::reduce($list, $iterator, $memo, $context); }
/** * @tags collections */ public function testReduce() { // it should provide the memo, the value, the index and the list to the reduction function (in that order) $test = $this; _::reduce(['a' => 1, 'b' => 2, 'c' => 3], function ($m, $v, $k, $l) use($test) { $test->integer($v); $test->string($k); $test->array($l); $test->boolean(is_null($m))->isTrue(); return $m; }, null); $this->typeTolerant([1, 2, 3], 6, function ($in, $out) { $this->variable(_::reduce($in, function ($m, $v) { return $m + $v; }, 0))->isEqualTo($out); }, [0, -1]); // it should be possible to specify a context for reduction function $this->variable(_::reduce([1, 2, 3], function ($m, $v) { return ($m + $v) * $this->mult; }, 0, (object) ['mult' => 2]))->isEqualTo(22); }