示例#1
0
 function testIncrement()
 {
     /** @var $f callback */
     $f = F::increment();
     $this->assertSame(0, $f());
     $this->assertSame(1, $f());
     $this->assertSame(2, $f());
     $g = F::increment();
     $this->assertSame(0, $g());
     $this->assertSame(1, $g());
     $this->assertSame(3, $f());
 }
示例#2
0
 function assertEnumOrderEquals(array $expected, E $actual, $maxLength = PHP_INT_MAX)
 {
     $this->assertEquals($expected, $actual->take($maxLength)->select('array($k, $v)', Functions::increment()));
 }
示例#3
0
 /**
  * <p><b>Syntax</b>: toValues ()
  * <p>Returns a sequence of values from the source sequence; keys are discarded.
  * @return Enumerable A sequence with the same values and sequental integers as keys.
  * @see array_values
  */
 public function toValues()
 {
     return $this->select(Functions::$value, Functions::increment());
 }
示例#4
0
 /** @covers YaLinqo\Enumerable::join
  */
 function testJoin()
 {
     // join (inner)
     $this->assertEnumEquals(array(), E::from(array())->join(array()));
     $this->assertEnumEquals(array(), E::from(array())->join(array(6, 7, 8)));
     $this->assertEnumEquals(array(), E::from(array(3, 4, 5))->join(array()));
     $this->assertEnumEquals(array(a(3, 6), a(4, 7), a(5, 8)), E::from(array(3, 4, 5))->join(array(6, 7, 8)));
     $this->assertEnumEquals(array('a' => a(3, 6), 'b' => a(4, 7), 'c' => a(5, 8)), E::from(array('a' => 3, 'b' => 4, 'c' => 5))->join(array('a' => 6, 'b' => 7, 'c' => 8)));
     // join (inner, outerKeySelector)
     $this->assertEnumEquals(array(3 => a(a(3, 4), 6), 6 => a(a(5, 6), 7), 9 => a(a(7, 8), 8)), E::from(array(a(3, 4), a(5, 6), a(7, 8)))->join(array(3 => 6, 6 => 7, 9 => 8), '$v[0]+$k'));
     // join (inner, outerKeySelector, innerKeySelector)
     $this->assertEnumEquals(array(4 => a(1, 3), 6 => a(2, 4), 8 => a(3, 5)), E::from(array(4 => 1, 6 => 2, 8 => 3))->join(array(1 => 3, 2 => 4, 3 => 5), null, '$v+$k'));
     $this->assertEnumEquals(array(4 => a(4, 3), 6 => a(6, 4), 8 => a(8, 5)), E::from(array(3 => 4, 5 => 6, 7 => 8))->join(array(1 => 3, 2 => 4, 3 => 5), '$v', '$v+$k'));
     // join (inner, outerKeySelector, innerKeySelector, resultSelectorValue)
     $this->assertEnumEquals(array(a(3, 6), a(5, 7), a(7, 8)), E::from(array(3, 4, 5))->join(array(6, 7, 8), null, null, 'array($v1+$k, $v2)'));
     $this->assertEnumEquals(array(1 => a(6, 3), 2 => a(7, 4), 3 => a(8, 5)), E::from(array('a1' => 3, 'a2' => 4, 'a3' => 5))->join(array('1b' => 6, '2b' => 7, '3b' => 8), '$k[1]', 'intval($k)', 'array($v2, $v1)'));
     // join (inner, outerKeySelector, innerKeySelector, resultSelectorValue, resultSelectorKey)
     $this->assertEnumOrderEquals(array(a(6, 'a'), a(7, 'b'), a(7, 'c')), E::from(array(a(1, 6), a(2, 7), a(3, 8)))->join(array(a(1, 'a'), a(2, 'b'), a(2, 'c'), a(4, 'd')), '$v[0]', '$v[0]', '$v2[1]', '$v1[1]'));
     $this->assertEnumEquals(array(a(6, 'a'), a(7, 'b'), a(7, 'c')), E::from(array(a(1, 6), a(2, 7), a(3, 8)))->join(array(a(1, 'a'), a(2, 'b'), a(2, 'c'), a(4, 'd')), '$v[0]', '$v[0]', 'array($v1[1], $v2[1])', Functions::increment()));
 }
示例#5
0
 public static function assertEnumOrderEquals(array $expected, E $actual, $maxLength = PHP_INT_MAX)
 {
     self::assertEquals($expected, $actual->take($maxLength)->select('[ $k, $v ]', Functions::increment())->toArrayDeep());
 }
示例#6
0
 /** @covers YaLinqo\Enumerable::join
  */
 function testJoin()
 {
     // join (inner)
     $this->assertEnumEquals([], E::from([])->join([]));
     $this->assertEnumEquals([], E::from([])->join([6, 7, 8]));
     $this->assertEnumEquals([], E::from([3, 4, 5])->join([]));
     $this->assertEnumEquals([[3, 6], [4, 7], [5, 8]], E::from([3, 4, 5])->join([6, 7, 8]));
     $this->assertEnumEquals(['a' => [3, 6], 'b' => [4, 7], 'c' => [5, 8]], E::from(['a' => 3, 'b' => 4, 'c' => 5])->join(['a' => 6, 'b' => 7, 'c' => 8]));
     // join (inner, outerKeySelector)
     $this->assertEnumEquals([3 => [[3, 4], 6], 6 => [[5, 6], 7], 9 => [[7, 8], 8]], E::from([[3, 4], [5, 6], [7, 8]])->join([3 => 6, 6 => 7, 9 => 8], '$v[0]+$k'));
     // join (inner, outerKeySelector, innerKeySelector)
     $this->assertEnumEquals([4 => [1, 3], 6 => [2, 4], 8 => [3, 5]], E::from([4 => 1, 6 => 2, 8 => 3])->join([1 => 3, 2 => 4, 3 => 5], null, '$v+$k'));
     $this->assertEnumEquals([4 => [4, 3], 6 => [6, 4], 8 => [8, 5]], E::from([3 => 4, 5 => 6, 7 => 8])->join([1 => 3, 2 => 4, 3 => 5], '$v', '$v+$k'));
     // join (inner, outerKeySelector, innerKeySelector, resultSelectorValue)
     $this->assertEnumEquals([[3, 6], [5, 7], [7, 8]], E::from([3, 4, 5])->join([6, 7, 8], null, null, '[ $v1+$k, $v2 ]'));
     $this->assertEnumEquals([1 => [6, 3], 2 => [7, 4], 3 => [8, 5]], E::from(['a1' => 3, 'a2' => 4, 'a3' => 5])->join(['1b' => 6, '2b' => 7, '3b' => 8], '$k[1]', 'intval($k)', '[ $v2, $v1 ]'));
     // join (inner, outerKeySelector, innerKeySelector, resultSelectorValue, resultSelectorKey)
     $this->assertEnumOrderEquals([[6, 'a'], [7, 'b'], [7, 'c']], E::from([[1, 6], [2, 7], [3, 8]])->join([[1, 'a'], [2, 'b'], [2, 'c'], [4, 'd']], '$v[0]', '$v[0]', '$v2[1]', '$v1[1]'));
     $this->assertEnumEquals([[6, 'a'], [7, 'b'], [7, 'c']], E::from([[1, 6], [2, 7], [3, 8]])->join([[1, 'a'], [2, 'b'], [2, 'c'], [4, 'd']], '$v[0]', '$v[0]', '[ $v1[1], $v2[1] ]', Functions::increment()));
 }
示例#7
0
 function testIncrement()
 {
     $f = F::increment();
     $this->assertSame(0, $f());
     $this->assertSame(1, $f());
     $this->assertSame(2, $f());
     $g = F::increment();
     $this->assertSame(0, $g());
     $this->assertSame(1, $g());
     $this->assertSame(3, $f());
 }
示例#8
0
文件: Linq.php 项目: athari/yalinqo
<?php

/**
 * Global functions and initialization.
 * @author Alexander Prokhorov
 * @license Simplified BSD
 * @link https://github.com/Athari/YaLinqo YaLinqo on GitHub
 */
\YaLinqo\Functions::init();
\YaLinqo\Utils::init();
if (!function_exists('from')) {
    /**
     * Create Enumerable from an array or any other traversible source.
     * @param array|\Iterator|\IteratorAggregate|\YaLinqo\Enumerable $source
     * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable.
     * @return \YaLinqo\Enumerable
     * @see \YaLinqo\Enumerable::from
     */
    function from($source)
    {
        return \YaLinqo\Enumerable::from($source);
    }
}
示例#9
0
        self::$blank = function () {
        };
        self::$compareStrict = function ($a, $b) {
            if ($a === $b) {
                return 0;
            } elseif ($a > $b) {
                return 1;
            } else {
                return -1;
            }
        };
        self::$compareLoose = function ($a, $b) {
            if ($a == $b) {
                return 0;
            } elseif ($a > $b) {
                return 1;
            } else {
                return -1;
            }
        };
    }
    public static function increment()
    {
        $i = 0;
        return function () use(&$i) {
            return $i++;
        };
    }
}
Functions::init();