Exemplo n.º 1
0
 /**
  * Creates a new iterator over a sequence
  *
  * @param  util.data.Sequence $seq
  * @throws lang.IllegalStateException If the sequence has been processed
  */
 public function __construct(Sequence $seq)
 {
     $this->it = $seq->getIterator();
     try {
         $this->it->rewind();
     } catch (\Exception $e) {
         throw new IllegalStateException($e->getMessage());
     }
 }
 public function flatten_generator_with_key()
 {
     $this->assertSequence([3, 6], Sequence::of([1 => 2])->flatten(function ($n, $key) {
         (yield $n + $key);
         (yield $n * ($n + $key));
     }));
 }
 /**
  * Lazy fetching sequence
  *
  * @param  int $limit
  * @param  int $page
  * @return util.data.Sequence
  */
 protected function records($limit = self::PAGE, $page = 0)
 {
     $records = $this->loadPage($page * $limit, $limit);
     return Sequence::concat($records, sizeof($records) < $limit ? null : function () use($page, $limit) {
         return $this->records($limit, ++$page);
     });
 }
 public function concat_iteratively()
 {
     $seq = Sequence::$EMPTY;
     foreach ([[1, 2], [3, 4], [5, 6]] as $array) {
         $seq = Sequence::concat($seq, Sequence::of($array));
     }
     $this->assertSequence([1, 2, 3, 4, 5, 6], $seq);
 }
 public function with_generator_and_key()
 {
     $records = Sequence::of(['color' => 'green', 'price' => 12.99]);
     $generator = function ($value, $key) {
         (yield strtoupper($key) => $value);
     };
     $this->assertEquals(['COLOR' => 'green', 'PRICE' => 12.99], $records->map($generator)->toMap());
 }
Exemplo n.º 6
0
 public function min_max_sum_average_and_count_for_non_empty()
 {
     Sequence::of([1, 2, 3, 4])->collecting($min, Aggregations::min())->collecting($max, Aggregations::max())->collecting($average, Aggregations::average())->collecting($sum, Aggregations::sum())->collecting($count, Aggregations::count())->each();
     $this->assertEquals(1, $min);
     $this->assertEquals(4, $max);
     $this->assertEquals(2.5, $average);
     $this->assertEquals(10, $sum);
     $this->assertEquals(4, $count);
 }
 public function map_key_is_passed_to_function()
 {
     $keys = [];
     Sequence::of(['one' => 1, 'two' => 2, 'three' => 3])->filter(function ($e, $key) use(&$keys) {
         $keys[] = $key;
         return true;
     })->each();
     $this->assertEquals(['one', 'two', 'three'], $keys);
 }
 public function at_end($input)
 {
     $it = Sequence::of($input)->getIterator();
     $it->rewind();
     $it->next();
     $it->next();
     $it->next();
     $this->assertEquals([], iterator_to_array(new ContinuationOf($it)));
 }
 public function used_for_joining()
 {
     $result = Sequence::of(['a', 'b', 'c'])->collect(new Collector(function () {
         return '';
     }, function (&$result, $arg) {
         $result .= ', ' . $arg;
     }, function ($result) {
         return substr($result, 2);
     }));
     $this->assertEquals('a, b, c', $result);
 }
Exemplo n.º 10
0
 /**
  * Add a measurable class
  *
  * @param  var $class Either a string or an `XPClass` instance
  * @return self This
  * @throws lang.IllegalArgumentException If the class is not a subclass of util.profiling.Measurable
  */
 public function measuring($type)
 {
     $class = $type instanceof XPClass ? $type : XPClass::forName($type);
     if (!$class->isSubclassOf('util.profiling.Measurable')) {
         throw new IllegalArgumentException($class->toString() . ' must be a subclass of util.profiling.Measurable');
     }
     $this->measurables = Sequence::of($class->getMethods())->filter(self::$ANNOTATED)->map(function ($method) use($class) {
         if ($method->hasAnnotation('values')) {
             return $this->permutationOf($class, $method, $method->getAnnotation('values'));
         } else {
             return [$class->newInstance($method)];
         }
     })->flatten();
     return $this;
 }
Exemplo n.º 11
0
 /**
  * Returns fixed enumerables, that is, those that can be rewound.
  *
  * @return var[][]
  */
 public static function fixedMaps()
 {
     return [[['color' => 'green', 'price' => 12.99], 'map'], [new \ArrayObject(['color' => 'green', 'price' => 12.99]), 'iterable'], [new \ArrayIterator(['color' => 'green', 'price' => 12.99]), 'iterator'], [Sequence::of(['color' => 'green', 'price' => 12.99]), 'self']];
 }
Exemplo n.º 12
0
 public function raises_exception_when_given_null_and_args()
 {
     Sequence::of([])->each(null, []);
 }
 /**
  * Assertion helper
  *
  * @param  var[] $expected
  * @param  util.data.Sequence $sequence
  * @param  string $message
  * @throws unittest.AssertionFailedError
  */
 protected function assertSequence($expected, $sequence, $message = '!=')
 {
     $this->assertEquals($expected, $sequence->toArray(), $message);
 }
Exemplo n.º 14
0
 public function max_using_closure()
 {
     $this->assertEquals(new Date('2014-07-17'), Sequence::of([new Date('1977-12-14'), new Date('2014-07-17'), new Date('1979-12-29')])->max(function ($a, $b) {
         return $b->compareTo($a);
     }));
 }
Exemplo n.º 15
0
 public function summing_with_function_and_names($key, $expect)
 {
     $pivot = Sequence::of($this->measurements())->collect((new InPivot())->groupingBy('type')->summing(function ($row) {
         return $row['occurrences'];
     }, $key));
     $this->assertEquals($expect, $pivot->sum('bad'));
 }
Exemplo n.º 16
0
 public function flattens_array_and_sequence()
 {
     $this->assertEquals([1, 2, 3, 4], $this->flatten([[1, 2], Sequence::of([3, 4])]));
 }
 public function used_for_concatenation()
 {
     $this->assertEquals('Hello World', Sequence::of(['Hello', ' ', 'World'])->reduce('', function ($a, $b) {
         return $a . $b;
     }));
 }
Exemplo n.º 18
0
 public function receives_offset()
 {
     $this->assertSequence([3, 4], Sequence::of([1, 2, 3, 4])->skip(function ($e, $offset) {
         return $offset < 2;
     }));
 }
 public function sequence_of_iterator($input)
 {
     $this->assertSequence([1, 2, 3], Sequence::of(Sequence::of($input)->iterator()));
 }
Exemplo n.º 20
0
 public function receives_offset()
 {
     $this->assertSequence([1, 2], Sequence::of([1, 2, 3, 4])->limit(function ($e, $offset) {
         return $offset >= 2;
     }));
 }
Exemplo n.º 21
0
 public function raises_exception_when_given($noncallable)
 {
     Sequence::of([])->peek($noncallable);
 }
 public function passing_null_to_of_yields_an_empty_sequence()
 {
     $this->assertEquals(Sequence::$EMPTY, Sequence::of(null));
 }
Exemplo n.º 23
0
 public function toString_for_sequence_of_array()
 {
     $this->assertEquals('util.data.Sequence@[1, 2, 3]', Sequence::of([1, 2, 3])->toString());
 }
Exemplo n.º 24
0
 public function sorted_by_natural_order_string_comparison()
 {
     $this->assertSequence(['rfc1.txt', 'rfc822.txt', 'rfc2086.txt'], Sequence::of(['rfc1.txt', 'rfc2086.txt', 'rfc822.txt'])->sorted(function ($a, $b) {
         return strnatcasecmp($a, $b);
     }), 'http://sourcefrog.net/projects/natsort/');
 }
Exemplo n.º 25
0
 public function partitioningBy_handles_non_booleans()
 {
     $this->assertHashTable([true => new Vector(['Test', 'Unittest']), false => new Vector(['Trial & Error'])], Sequence::of(['Test', 'Unittest', 'Trial & Error'])->collect(Collectors::partitioningBy(function ($e) {
         return stristr($e, 'Test');
     })));
 }