예제 #1
0
 /** @covers YaLinqo\Enumerable::split
  */
 function testSplit()
 {
     // without empty
     $this->assertEnumEquals(array('123 4 44'), E::split('123 4 44', '#, ?#'));
     // with empty
     $this->assertEnumEquals(array('123', '4', '44', ''), E::split('123,4, 44,', '#, ?#'));
     // with empty, empty skipped
     $this->assertEnumEquals(array('123', '4', '44'), E::split('123,4, 44,', '#, ?#', PREG_SPLIT_NO_EMPTY));
     // with empty, empty skipped, no results
     $this->assertEnumEquals(array(), E::split(',', '#, ?#', PREG_SPLIT_NO_EMPTY));
 }
예제 #2
0
    return array($v[1], $v[0] + $v[1]);
}, array(1, 1), function ($v) {
    return $v[1];
}, 1)->toKeys()->take(10)->toArray());
var_dump(Enumerable::generate(function ($v, $k) {
    return pow(-1, $k) / (2 * $k + 1);
}, 0)->take(1000)->sum() * 4);
var_dump(Enumerable::toInfinity()->take(999)->sum(function ($k) {
    return pow(-1, $k) / (2 * $k + 1);
}) * 4);
echo "Lambdas\n";
var_dump(from(array(1, 2, 3, 4, 5, 6))->where('$v ==> $v > 3')->select('$v ==> $v*$v')->toArray());
var_dump(from(array(1, 2, 3, 4, 5, 6))->where('($v) ==> $v > 3')->select('$v, $k ==> $v+$k')->toArray());
var_dump(from(array(1, 2, 3, 4, 5, 6))->where('($v) ==> { echo $v; return $v > 3; }')->select('($v, $k) ==> { return $v*2+$k*3; }')->toArray());
var_dump(from(array(1, 2, 3, 4, 5, 6))->where('$v > 2')->where('$v>3')->select('$v+$k')->toArray());
var_dump(Enumerable::split('1 2 3 4 5 6', '# #')->toArray());
var_dump(Enumerable::matches('1 2 3 4 5 6', '#\\d+#')->select('$v[0]')->maxBy(Functions::$compareStrict));
var_dump(from(array(1, 2))->selectMany('$v ==> array(1, 2)', '"$v1 $v2"', '"$k1 $k2"')->toArray());
var_dump(from(array(1, 2))->selectMany('$v ==> array(1, 2)', '"$k1=$v1 $k2=$v2"')->toArray());
var_dump(from(array(1, 2))->selectMany('$v ==> array(1, 2)', 'array($v1, $v2)')->toArray());
var_dump(from(array(1, 2))->selectMany('$v ==> array()', '"$v1 $v2"', '"$k1 $k2"')->toArray());
var_dump(from(array())->selectMany('$v ==> array(1, 2)', '"$v1 $v2"', '"$k1 $k2"')->toArray());
var_dump(from(array('a' => array(1, 2), 'b' => array(3)))->selectMany('$v')->toArray());
var_dump(from(array(array(1, 3, 4), array(2, 1, 4), array(2, 1, 1), array(2, 3, 1), array(1, 3, 1), array(1, 1, 1)))->orderBy('$v[0]')->thenBy('$v[1]')->thenByDescending('$v[2]')->select('implode(" ", $v)')->toList());
var_dump(from(array(1, 1, 1, 2, 2, 3))->select('$v', '$v')->toLookup());
var_dump(from(array('a' => 1, 'b' => 2))->toObject());
var_dump(from(array('a', 'b', 'c', 10 => 'z'))->join(array('d', 'e', 10 => 'y', 11 => 'x'))->toArray());
var_dump(from(array(array('id' => 10, 'name' => 'cat1'), array('id' => 11, 'name' => 'cat2'), array('id' => 12, 'name' => 'cat3')))->join(array(array('name' => 'prod1', 'catId' => 10), array('name' => 'prod2', 'catId' => 10), array('name' => 'prod3', 'catId' => 11), array('name' => 'prod4', 'catId' => 13)), '$v["id"]', '$v["catId"]', function ($cat, $prod) {
    return "prod {$prod['name']} from {$cat['name']}";
})->toLookup()->toArray());
var_dump(from(array('a', 'b', 'c', 10 => 'z'))->groupJoin(array('d', 'e', 10 => 'y', 11 => 'x'), null, null, 'array($v, $e->toArray())')->toArray());