ebeacon/iterutil
Composer 安装命令:
composer require ebeacon/iterutil
包简介
Creates iterators for arrays or any iterable, with lazily evaluated (memory efficient) transformations. Heavily inspired by Rust's iterators.
README 文档
README
IterUtil is an iterator builder for PHP that enables you to apply a variety of
transformations on the data held in any iterable without modifying the
original's contents. All "non-consuming" methods are lazily evaluated, saving
memory usage as well as CPU time. It is mostly inspired by Rust's Iterator
trait, with some
alterations made for PHP coding conventions and its somewhat unique situation
with associative arrays.
At present, it supports PHP all the way back through 5.6, though 7.4+ is certainly recommended if only to have access to the arrow function syntax!
General Constructor
$fruits = ["apples", "bananas", "oranges"];
$iter = IterUtil::from($fruits);
IterUtil::from creates a new IterUtil instance from any array, or any object
that implements Traversable. At this point, it is effectively just an iterator
wrapping the iterator you provided. No other work is performed, but now you can
apply transformations.
Transformations
$vowels = ["A", "E", "I", "O", "U"];
$fruits = ["Apples", "Bananas", "oranges"];
$iter = IterUtil::from($fruits)
->map(fn($fruit) => ucfirst($fruit))
->filter(fn($fruit) => in_array(substr($fruit, 0, 1), $vowels))
->keys();
Transformations are both lazy and chainable. All that we've done is modify the
iterator itself. $fruits is unchanged and we haven't even begun to iterate
yet.
Consumers
Other methods will consume the iterator, perform any transformations in the
order in which they were applied, and produce some sort of result. One of these,
collect(), produces an array:
$vowels = ["A", "E", "I", "O", "U"];
$fruits = ["Apples", "Bananas", "oranges"];
$result = IterUtil::from($fruits)
->map(fn($fruit) => ucfirst($fruit))
->filter(fn($fruit) => in_array(substr($fruit, 0, 1), $vowels))
->keys()
->collect();
$result is now equal to [0, 2], which are the keys of the fruits that begin
with vowels. The original list in $fruits is actually only iterated over a
single time, and is the equivalent of writing:
$vowels = ["A", "E", "I", "O", "U"];
$fruits = ["Apples", "Bananas", "oranges"];
$result = [];
foreach ($fruits as $key => $fruit) {
if (in_array(substr(ucfirst($fruit), 0, 1), $vowels)) {
$result[] = $key;
}
}
reduce() is another common example:
$cart = [
["name" => "Apples", "qty" => 2, "price" => .30],
["name" => "Bananas", "qty" => 1, "price" => 10.00],
["name" => "Oranges", "qty" => 3, "price" => .45],
];
$subtotal = IterUtil::from($cart)
->reduce(fn($acc, $item) => $acc + ($item["qty"] * $item["price"]), 0);
Specialized Constructors
IterUtil::fromString($str, $delimiter = "") will allow you to iterate over a
string, split on the delimiter. (If the delimiter is an empty string or not
provided, it will split the string into individual characters.)
$hello = "Hello, World!";
$words = IterUtil::fromString($hello, " ")
->collect();
// $words is now equal to ["Hello,", "World!"]
IterUtil::range($start = 0, $end = PHP_INT_MAX, $step = 1) can generate an
(inclusive) range of numbers.
$numbers = IterUtil::range(0, 7)
->collect();
// $numbers is now equal to [0, 1, 2, 3, 4, 5, 6, 7]
IterUtil::repeat($value, $n = INF) will repeat the same value over and over
again.
$aBunchOfZeroes = IterUtil::repeat(0, 8)
->collect();
// $aBunchOfZeroes is now equal to [0, 0, 0, 0, 0, 0, 0, 0]
ebeacon/iterutil 适用场景与选型建议
ebeacon/iterutil 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 729 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ebeacon/iterutil 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ebeacon/iterutil 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 729
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-04