public function testReduce() { // from js $sum = __u::reduce(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'can sum up an array'); $context = array('multiplier' => 3); $sum = __u::reduce(array(1, 2, 3), function ($sum, $num) use($context) { return $sum + $num * $context['multiplier']; }, 0); $this->assertEquals(18, $sum, 'can reduce with a context object'); $sum = __u::reduce(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'default initial value'); $ifnull = null; try { __u::reduce(null, function () { }); } catch (Exception $e) { $ifnull = $e; } $this->assertFalse($ifnull === null, 'handles a null (without initial value) properly'); $this->assertEquals(138, __u::reduce(null, function () { }, 138), 'handles a null (with initial value) properly'); $sum = __u(array(1, 2, 3))->reduce(function ($sum, $num) { return $sum + $num; }); $this->assertEquals(6, $sum, 'OO-style reduce'); $sum = __u::inject(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'aliased as "inject"'); $sum = __u::foldl(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'aliased as "foldl"'); // docs $this->assertEquals(6, __u::reduce(array(1, 2, 3), function ($memo, $num) { return $memo + $num; }, 0)); }