public function testReduceRight() { // from js $list = __u::reduceRight(array('foo', 'bar', 'baz'), function ($memo, $str) { return $memo . $str; }, ''); $this->assertEquals('bazbarfoo', $list, 'can perform right folds'); $ifnull = null; try { __u::reduceRight(null, function () { }); } catch (Exception $e) { $ifnull = $e; } $this->assertFalse($ifnull === null, 'handles a null (without initial value) properly'); $this->assertEquals(138, __u::reduceRight(null, function () { }, 138), 'handles a null (with initial value) properly'); // extra $list = __u(array('moe', 'curly', 'larry'))->reduceRight(function ($memo, $str) { return $memo . $str; }, ''); $this->assertEquals('larrycurlymoe', $list, 'can perform right folds in OO-style'); $list = __u::foldr(array('foo', 'bar', 'baz'), function ($memo, $str) { return $memo . $str; }, ''); $this->assertEquals('bazbarfoo', $list, 'aliased as "foldr"'); $list = __u::foldr(array('foo', 'bar', 'baz'), function ($memo, $str) { return $memo . $str; }); $this->assertEquals('bazbarfoo', $list, 'default initial value'); // docs $list = array(array(0, 1), array(2, 3), array(4, 5)); $flat = __u::reduceRight($list, function ($a, $b) { return array_merge($a, $b); }, array()); $this->assertEquals(array(4, 5, 2, 3, 0, 1), $flat); }