Giter VIP home page Giter VIP logo

php-iterable-functions's Introduction

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

php-iterable-functions's People

Contributors

ben-synapse avatar bpolaszek avatar jdecool avatar mwijngaard avatar simpod avatar weph avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-iterable-functions's Issues

iterable_values

Do you have thoughts on iterable_values? I am missing that key-resetting partner-in-crime to array_values.

Drop EOLed PHP support

Hi, I'd like to refactor this a bit while dropping old PHP support (keeping 7.2+), bumping versions and adding type hints and templated annotations for psalm.

Let me know if interested. I'd do it in small PRs.

Proposal: iterable_to_splfixedarray

When iterable_to_traversable returns an ArrayIterator, a new iterable_to_splfixedarray would perfectly complement the iterable_to*_ functions. Admittedly, that name is somehow bulky โ€“ but in pseudocode it would go like this:

function iterable_to_splfixedarray( $things )
{
  return \SplFixedArray::fromArray( iterable_to_array( $things ), "with_indexes" );
}

What do you think?

Missing Namespace

Hey! These functions look very neat, I was just wondering whether there shouldn't be any namespace declared for this lib to avoid eventual conflicts. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.