iterator-tools/pipeline
Composer 安装命令:
composer require iterator-tools/pipeline
包简介
Tools to work with iterators including lazy evaluation
README 文档
README
Library provides tools for working with iterators in a convenient way. Promotes functional style
of programming when dealing with iterable collections.
Install using Composer:
composer require iterator-tools/pipeline
Example:
Allows defining iterators pipeline separately from actually consuming it. Allows processing large collections by avoiding loading all data into the memory at once - each key, value pair returned by source iterator is passed by multiple stages separately.
<?php use IteratorTools\Source\Csv\CsvReader; use IteratorTools\Source\Csv\CsvReaderOptions; use function IteratorTools\Iterator\pipeline; use function IteratorTools\Consumers\int_sum; $options = CsvReaderOptions::defaults()->withDateColumn('created_at', 'Y-m-d H:i'); // First you need an iterable source from which you can create a stream $csv = CsvReader::from('some-file.csv', $options)->readAssoc(); // Some helper object $deadline = new DateTime('2022-03-01 00:00'); // Here we create a "pipeline" using source Iterator $total = pipeline($csv) // First we tell that we want to filter all rows by date // (no iteration is happening at this moment) ->filter(function (array $row) use ($deadline) { return $row['created_at'] >= $deadline; }) // Then we tell that after filtering we want mapping to integer values // (still, no iteration happens here) ->map(fn($row) => (int)$row['count']) // And here we want to consume all integers // and compute an integer sum // (here $csvReader is consumed; one row from file // at a time is filtered, mapped and then summed) // This line returns integer value. // result: 70 ->consume(int_sum());
Example some-file.csv file for the code above:
id,count,created_at 1,30,"2022-01-01 12:00" 2,10,"2022-02-01 12:00" 3,50,"2022-03-01 12:00" 4,20,"2022-04-01 12:00"
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-29