zalas / collections
此包已被废弃,不再维护。未建议替代包。
一个微型的集合库
v1.1.0
2020-04-06 20:46 UTC
Requires
- php: ^7.3
Requires (Dev)
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2022-03-07 01:06:36 UTC
README
一个微型的集合库,允许在生成器和其它可遍历对象上进行类似 array_*
的操作。
集合接口
interface Collection extends IteratorAggregate { public function merge(Collection $other): Collection; public function filter(callable $f): Collection; public function map(callable $f): Collection; /** * @param mixed $initial * @param callable $f * * @return mixed */ public function reduce($initial, callable $f); }
集合实现
LazyCollection
懒集合实现将元素的评估推迟到必要时。
use Zalas\Collection\LazyCollection; $c = (new LazyCollection(new \ArrayIterator([1, 2, 3, 4]))) ->filter(fn (int $e) => 0 === $e % 2) ->map(fn (int $e) => $e * 2) ;
在旧版PHP版本中
use Zalas\Collection\LazyCollection; $c = (new LazyCollection(new \ArrayIterator([1, 2, 3, 4))) ->filter(function (int $e) { return 0 === $e % 2; }) ->map(function (int $e) { return $e * 2; }) ;
安装
composer require zalas/collections