xp-forge/sequence
Composer 安装命令:
composer require xp-forge/sequence
包简介
Data sequences
README 文档
README
This API allows working with data sequences of different kinds in a functional style, e.g. map/reduce.
Examples
Sequence
Instances of the util.data.Sequence class can be created from iterable input, either an in-memory structure or a stream of data, e.g. read from a network socket. The Sequence class provides intermediate (work on single elements, return a new Sequence) and terminal (consume all elements, returning a single value) operations.
use util\data\{Sequence, Collectors, Aggregations}; $return= Sequence::of([1, 2, 3, 4]) ->filter(fn($e) => 0 === $e % 2) ->toArray() ; // $return= [2, 4] $return= Sequence::of([1, 2, 3, 4]) ->map(fn($e) => $e * 2) ->toArray() ; // $return= [2, 4, 6, 8] $i= 0; $return= Sequence::of([1, 2, 3, 4]) ->counting($i) ->reduce(0, fn($a, $b) => $a + $b) ; // $i= 4, $return= 10 $names= Sequence::of($this->people) ->map('com.example.Person::name') ->collect(Collectors::joining(', ')) ; // $names= "Timm, Alex, Dude" $experience= Sequence::of($this->employees) ->collect(Collectors::groupingBy( fn($e) => $e->department(), Aggregations::average(fn($e) => $e->years()) )) ; // $experience= util.collections.HashTable[2] { // Department("A") => 12.8 // Department("B") => 3.5 // }
Optional
Instances of the util.data.Optional class are thin wrappers around possible NULL values. The operations provided by Optional class help in reducing conditional code:
use util\data\Optional; $first= Optional::of($repository->find($user)); if ($first->present()) { $user= $first->get(); // When Repository::find() returned non-null } $user= $first->orElse($this->currentUser); $user= $first->orUse(fn() => $this->currentUser()); $name= $first ->filter(fn($user) => $user->isActive()) ->whenAbsent($this->currentUser) ->whenAbsent(fn() => $this->guestUser()) ->map('com.example.User::name') ->get() ;
Creation operations
Sequences can be created from a variety of sources, and by using these static methods:
- of - accepts PHP arrays (zero-based as well as associative), all traversable data structures including
lang.types.ArrayListandlang.types.ArrayMapas well as anything fromutil.collections,util.XPIteratorinstances, PHP iterators and iterator aggregates, PHP 5.5 generators (yield), as well as sequences themselves. Passing NULL will yield an empty sequence. - iterate - Iterates starting with a given seed, applying a unary operator on this value and passing the result to the next invocation, forever. Combine with
limit()! - generate - Iterates forever, returning whatever the given supplier function returns. Combine with
limit()!
Intermediate operations
The following operations return a new Sequence instance on which more intermediate or terminal operations can be invoked:
- skip - Skips past elements in the beginning. Using
skip(4)skips the first four elements,skip(function($e) { return 'initial' === $e; })will skip all elements which equal to the string initial. - limit - Stops iteration when limit is reached. Using
limit(10)stops iteration once ten elements have been returned,limit(function($e) { return 'stop' === $e; })will stop once the first element equal to the string stop is encountered. - filter - Filters the sequence by a given criterion. The new sequence will only contain values for which it returns true. Accepts a function or a
util.Filterinstance. - map - Maps each element in the sequence by applying a function on it and returning a new sequence with the return value of that function.
- except - Returns the sequence with all values except the ones given.
- peek - Calls a function for each element in the sequence; especially useful for debugging, e.g.
peek('var_dump', []). - counting - Increments the integer given as its argument for each element in the sequence.
- collecting - Collects elements in this sequence to a
util.data.ICollectorinstance. Unlike the terminal operation below, passes the elements on. - flatten - Flattens sequences inside the sequence and returns a new list containing all values from all sequences.
- distinct - Returns a new sequence which only consists of unique elements. Uniqueness is calculated using the
util.Objects::hashOf()method by default (but can be passed another function). - zip - Combines values from this sequence with a given enumerable value, optionally using a given transformation function.
- concat - Concatenates this sequence with a given enumerable.
- sorted - Returns a sorted collection. Can be invoked with a comparator function, a
util.Comparatorinstance or the sort flags from PHP's sort() function (e.g.SORT_NUMERIC | SORT_DESC). - chunked - Returns a chunked stream with chunks not exceeding the given size. The last chunk may have a smaller size.
- windowed - Returns a sliding window stream - a list of element ranges that you would see if you were looking at the collection through a sliding window of the given size.
Terminal operations
The following operations return a single value by consuming all of the sequence:
- toArray - will return a PHP array with zero-based keys
- toMap - will return a PHP associative array
- first - will return the first element as an
util.data.Optionalinstance. A value will be present if the sequence was not empty. - single - like
first(), but raises an exception if more than one element is contained in the sequence. - count - will return the number of elements in the sequence
- min - returns the smalles element. Compares numbers by default but may be given a comparator function or a
util.Comparatorinstance. - max - same as
min(), but returns the largest element instead. - each - Applies a given function to each element in the sequence, and returns the number of elements. Can be invoked without a function to consume "silently".
- reduce - Perform a reduction on the elements in this sequence.
- collect - Pass all elements in this sequence to a
util.data.ICollectorinstance.
Iteration
To use controlled iteration on a sequence, you can use the foreach statement or receive a "hasNext/next"-iterator via the iterator() accessor. If the sequence is based on seekable data (rule of thumb: all in-memory structures will be seekable), these operations can be repeated with the same effect. Otherwise, a util.data.CannotReset exception will be raised (e.g., for data streamed from a socket).
Further reading
- The java.util.stream package
- JDK8: Stream style - by Sergey Kuksenko, Performance Engineering at Oracle on Dec 03, 2013
- Processing Data with Java SE 8 Streams, Part 1
- Lazy sequences implementation for Java 8
xp-forge/sequence 适用场景与选型建议
xp-forge/sequence 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.06k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-forge/sequence 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-forge/sequence 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-forge/sequence 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
bootstrap select field module implementing http://silviomoreto.github.io/bootstrap-select/
Yii2 module to manage website content. Kind of a CMS...
Codeurx Modular is simply a package for Laravel to help you create and manage modules.
User Module for the Attogram Framework
Info Module for the Attogram Framework
Contact Form Module for the Attogram Framework
统计信息
- 总下载量: 14.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-09-27

