robinkanters/

streams-php

Java8 Streams库的移植。让PHP更加酷。

1.3 2016-10-23 19:09 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:51 UTC


README

Build Status SensioLabsInsight

Streams是PHP的Streams库的移植。它使得操作集合变得非常愉快。

安装

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

"robinkanters/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,并确保构建通过。