public function testMapCollector() { $array = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6]; $stream = new Stream($array); $stream->map(new SquareFunction()); $res = $stream->collect(new MapCollector()); $this->assertEquals(['a' => 1, 'b' => 4, 'c' => 9, 'd' => 16, 'e' => 25, 'f' => 36], $res); }
<?php require_once '../../src/autoload.php'; use phpstream\Stream; $stream = new Stream(['0', 'aa', '1', 'bb', '2', 'cc', '3', 'dd', '4']); $res = $stream->map(function ($value) { return intval($value); })->filter(function ($value) { return $value > 0; })->filter(function ($value) { return $value % 2 == 0; })->map(function ($value) { return $value + 1; })->reduceWithDefault(function ($acc, $v) { return $acc + $v; }, 0); // Returns 8 ! var_dump($res);