pepegar/streams-php

Java8 Streams 库的移植。让我们让PHP更酷。

1.1 2014-07-03 20:46 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:01:15 UTC


README

Build Status License SensioLabsInsight

Streams 是 PHP 版本的 Streams 库的移植。它使得处理集合变得非常愉快。

安装

只需将以下内容添加到您的 composer.json 文件中

"pepegar/streams-php": "dev-master"

用法

使用 Yii ActiveRecord 库的示例

<?php

use Streams as S;

$collection = Car::model()->findAll(); // Returns an array of Car objects
$carStream = new S\Stream($collection);

$newCollection = $carStream
    ->filter(function( $car ) {
		return ($car->price > 36000); // return only expensive cars!
	})->map(function( $car ) {
		$car->setCo2EmissionTaxes(20); // Since is an expensive car, lets make the people who drive it more poor :D
		return $car;
	})->getElements();

可用函数

尽管这个库正在积极开发中,但目前可用的方法包括

###map(callable $callback) 在函数式编程语言中,map 接受一个函数作为参数,并将其应用于数组中的每个元素,返回一个包含结果的新数组。

###filter(callable $callback) 接受一个函数作为参数,并将其应用于集合中的每个元素。它返回一个包含所有回调返回 true 的元素的新集合。

###allMatch(callable $predicate) 返回流中所有元素是否都匹配给定的谓词

###anyMatch(callable $predicate) 返回流中是否有任何元素匹配给定的谓词

###concat(Stream $a, Stream $b) 创建一个懒加载的连接流,其元素是第一个流中的所有元素,后面跟着第二个流中的所有元素。

###count() 返回流中的元素数量

###distinct() 返回一个包含流中不同元素的新流

开发

请提交您的 Pull Requests,并确保构建通过。