dgame / php-iterator
php 迭代器
v0.4.0
2021-10-23 21:18 UTC
Requires
- php: ^8.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.4
- ergebnis/phpstan-rules: ^0.15
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/phpstan: ^0.12
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^9.4
- roave/security-advisories: dev-latest
- slevomat/coding-standard: dev-master
- spaceemotion/php-coding-standard: dev-master
- spaze/phpstan-disallowed-calls: ^1.5
- symplify/easy-coding-standard: ^9.3
- thecodingmachine/phpstan-safe-rule: ^1.0
- thecodingmachine/phpstan-strict-rules: ^0.12
README
享受高阶函数
仅 & 重复
$this->assertEquals('aaaa', only('a')->repeat(4)->implode());
取
$this->assertEquals('Hal', chars('Hallo')->take(3)->implode());
跳过
$this->assertEquals('lo', chars('Hallo')->skip(3)->implode());
切片
$this->assertEquals('oBar', chars('FooBarQuatz')->slice(2, 6)->implode());
块
$this->assertEquals([['F', 'o'], ['o', 'B'], ['a', 'r']], chars('FooBar')->chunks(2)->collect());
折叠
$it = iter([6, 7, 8]); $sum1 = function($sum, int $a) { return $sum + $a; }; $sum2 = function(int $sum, int $a) { return $sum + $a; }; $this->assertEquals(21, $it->fold($sum1)); $this->assertEquals(63, $it->fold($sum2, 42));
取直到
$belowTen = function (int $item) { return $item < 10; }; $this->assertEquals([0, 1, 2], iter([0, 1, 2, 10, 20])->takeWhile($belowTen)->collect());
跳过直到
$belowTen = function (int $item) { return $item < 10; }; $this->assertEquals([10, 20], iter([0, 1, 2, 10, 20])->skipWhile($belowTen)->collect());
之前
$this->assertEquals('ab', chars('abcdef')->before('c')->implode());
$this->assertEquals(['a' => 'z', 'b' => 'y'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->before('x')->collect());
之后
$this->assertEquals('ef', chars('abcdef')->after('d')->implode());
$this->assertEquals(['d' => 'w'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->after('x')->collect());
从
$this->assertEquals('def', chars('abcdef')->from('d')->implode());
$this->assertEquals(['c' => 'x', 'd' => 'w'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->from('x')->collect());
直到
$this->assertEquals('abc', chars('abcdef')->until('c')->implode());
$this->assertEquals(['a' => 'z', 'b' => 'y', 'c' => 'x'], iter(['a' => 'z', 'b' => 'y', 'c' => 'x', 'd' => 'w'])->until('x')->collect());
所有
$positive = function (int $item) { return $item >= 0; }; $this->assertTrue(iter([0, 1, 2, 3])->all($positive)); $this->assertFalse(iter([-1, 2, 3, 4])->all($positive));
任何
$positive = function (int $item) { return $item > 0; }; $this->assertTrue(iter([-1, 0, 1])->any($positive)); $this->assertFalse(iter([-1])->any($positive));