コード例 #1
0
 public function testUnderscoreFindsRightClassToCall()
 {
     $numbers = array(3, 4, 5);
     $product = Underscore::reduce($numbers, function ($w, $v) {
         return $w * $v;
     }, 1);
     $this->assertEquals(60, $product);
 }
コード例 #2
0
function _reduce($list, $iterator, $memo, $context = NULL)
{
    return Underscore::reduce($list, $iterator, $memo, $context);
}
コード例 #3
0
ファイル: Underscore.php プロジェクト: brombal/underscore.php
 /**
  * @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);
 }