bentools/iterable-functions 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bentools/iterable-functions

Composer 安装命令:

composer require bentools/iterable-functions

包简介

Provides functions for iterable variables: is_iterable(), iterable_to_array()

README 文档

README

Latest Stable Version GitHub Actions Code Coverage Shepherd Type Total Downloads

Iterable functions

This package provides functions to work with iterables, as you usually do with arrays:

iterable_to_array()

PHP offers an iterator_to_array() function to export any iterator into an array.

But when you want to transform an iterable to an array, the iterable itself can already be an array.

When using iterator_to_array() with an iterable, that happens to be an array, PHP will throw a TypeError.

If you need an iterable-agnostic function, try our iterable_to_array():

use function BenTools\IterableFunctions\iterable_to_array;

var_dump(iterable_to_array(new \ArrayIterator(['foo', 'bar']))); // ['foo', 'bar']
var_dump(iterable_to_array(['foo', 'bar'])); // ['foo', 'bar']

iterable_to_traversable()

Useful when you have a Traversable type-hint, and you don't know wether or not your argument will be an array or an iterator.

If your variable is already an instance of Traversable (i.e. an Iterator, an IteratorAggregate or a Generator), the function simply returns it directly.

If your variable is an array, the function converts it to an ArrayIterator.

Usage:

use function BenTools\IterableFunctions\iterable_to_traversable;

var_dump(iterable_to_traversable(['foo', 'bar'])); // \ArrayIterator(['foo', 'bar'])
var_dump(iterable_to_traversable(new \ArrayIterator(['foo', 'bar']))); // \ArrayIterator(['foo', 'bar'])

iterable_map()

Works like an array_map with an array or a Traversable.

use function BenTools\IterableFunctions\iterable_map;

$generator = function () {
    yield 'foo';
    yield 'bar';
};

foreach (iterable_map($generator(), 'strtoupper') as $item) {
    var_dump($item); // FOO, BAR
}

iterable_merge()

Works like an array_merge with an array or a Traversable.

use function BenTools\IterableFunctions\iterable_merge;

$generator1 = function () {
    yield 'foo';
};

$generator2 = function () {
    yield 'bar';
};

foreach (iterable_merge($generator1(), $generator2()) as $item) {
    var_dump($item); // foo, bar
}

iterable_reduce()

Works like an reduce with an iterable.

use function BenTools\IterableFunctions\iterable_reduce;

$generator = function () {
    yield 1;
    yield 2;
};

$reduce = static function ($carry, $item) {
    return $carry + $item;
};

var_dump(
    iterable_reduce($generator(), $reduce, 0))
); // 3

iterable_filter()

Works like an array_filter with an array or a Traversable.

use function BenTools\IterableFunctions\iterable_filter;

$generator = function () {
    yield 0;
    yield 1;
};

foreach (iterable_filter($generator()) as $item) {
    var_dump($item); // 1
}

Of course you can define your own filter:

use function BenTools\IterableFunctions\iterable_filter;

$generator = function () {
    yield 'foo';
    yield 'bar';
};

$filter = function ($value) {
    return 'foo' !== $value;
};


foreach (iterable_filter($generator(), $filter) as $item) {
    var_dump($item); // bar
}

iterable_values()

Works like an array_values with an array or a Traversable.

use function BenTools\IterableFunctions\iterable_values;

$generator = function () {
    yield 'a' => 'a';
    yield 'b' => 'b';
};

foreach (iterable_values($generator()) as $key => $value) {
    var_dump($key); // 0, 1
    var_dump($value); // a, b
}

iterable_chunk()

Here's an array_chunk-like function that also works with a Traversable.

use function BenTools\IterableFunctions\iterable_chunk;

$fruits = [
    'banana',
    'apple',
    'strawberry',
    'raspberry',
    'pineapple',
]
$fruits = (fn () => yield from $fruits)()
iterable_chunk($fruits, 2);

/*
  [
    ['banana', 'apple'],
    ['strawberry', 'raspberry'],
    ['pineapple'],
  ]
 */

Iterable fluent interface

The iterable function allows you to wrap an iterable and apply some common operations.

With an array input:

use function BenTools\IterableFunctions\iterable;
$data = [
    'banana',
    'pineapple',
    'rock',
];

$iterable = iterable($data)->filter(fn($eatable) => 'rock' !== $eatable)->map('strtoupper'); // Traversable of ['banana', 'pineapple']

With a traversable input:

use function BenTools\IterableFunctions\iterable;
$data = [
    'banana',
    'pineapple',
    'rock',
];

$data = fn() => yield from $data;

$iterable = iterable($data())->filter(fn($eatable) => 'rock' !== $eatable)->map('strtoupper'); // Traversable of ['banana', 'pineapple']

Array output:

$iterable->asArray(); // array ['banana', 'pineapple']

Installation

composer require bentools/iterable-functions:^2.0

For PHP5+ compatibility, check out the 1.x branch.

Unit tests

php vendor/bin/pest

bentools/iterable-functions 适用场景与选型建议

bentools/iterable-functions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 437.89k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2017 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bentools/iterable-functions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bentools/iterable-functions 我们能提供哪些服务?
定制开发 / 二次开发

基于 bentools/iterable-functions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 437.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 20
  • 依赖项目数: 25
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-10