1. Go to this page and download the library: Download bentools/iterable-functions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bentools / iterable-functions example snippets
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']
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'])
use function BenTools\IterableFunctions\iterable_map;
$generator = function () {
yield 'foo';
yield 'bar';
};
foreach (iterable_map($generator(), 'strtoupper') as $item) {
var_dump($item); // FOO, BAR
}
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
}
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
use function BenTools\IterableFunctions\iterable_filter;
$generator = function () {
yield 0;
yield 1;
};
foreach (iterable_filter($generator()) as $item) {
var_dump($item); // 1
}
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
}
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
}