hguenot/phpstream
最新稳定版本:V3.0.0
Composer 安装命令:
composer require hguenot/phpstream
包简介
PHPStream claims to be a partial PHP port of the Java Stream API.
README 文档
README
PHP Stream is a basic port of new Java Stream API in PHP.
This library could be used to filter, convert or reduce any array.
Continuous integration
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist hguenot/phpstream "*"
or add
"hguenot/phpstream": "*"
to the require section of your composer.json file.
Usage
- Here is a basic usage of this library.
// Create a simple array of data $array = [ -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; // Creates a Stream based on the previous array $stream = new \phpstream\Stream($array); // Compute the opposite value of each value $stream = $stream->map(function($value){ return intval($value) * (-1); }); // Get only odd values $stream = $stream->filter(function($value){ return (intval($value) % 2) == 0; }); // Collects data into an array $new_array = $stream->collect(new \phpstream\collectors\ListCollector()); // Computes sum of all elements $sum = $stream->reduce(function($a, $b){ return intval($a) + intval($b); }, 0);
- All stream operations can be chained :
$sum = \phpstream\Stream::of($array) ->map(function($value){ return intval($value) * (-1); }) ->filter(function($value){ return (intval($value) % 2) == 0; }) ->reduce(function($a, $b){ return intval($a) + intval($b); }, 0);
You can find more examples on PHPUnit test.
统计信息
- 总下载量: 602
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-21