PHP code example of improved / iterable
1. Go to this page and download the library: Download improved/iterable 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/ */
improved / iterable example snippets
use Improved as i;
$filteredValues = i\iterable_filter($values, function($value) {
return is_int($value) && $value > 10;
});
$uniqueValues = i\iterable_unique($filteredValues);
$mappedValues = i\iterable_map($uniqueValues, function($value) {
return $value * $value - 1;
});
$firstValues = i\iterable_slice($mappedValues, 0, 10);
$result = i\iterable_to_array($firstValues);
use Improved\IteratorPipeline\Pipeline;
$result = Pipeline::with($values)
->filter(function($value) {
return is_int($value) && $value < 10;
})
->unique()
->map(function($value) {
return $value * $value - 1;
})
->limit(10)
->toArray();
use Improved\IteratorPipeline\Pipeline;
Pipeline::with([
new Person("Max", 18),
new Person("Peter", 23),
new Person("Pamela", 23)
]);
$dirs = new Pipeline(new \DirectoryIterator('some/path'));
use Improved\IteratorPipeline\Pipeline;
$blueprint = Pipeline::build()
->checkType('string')
->filter(function(string $value): bool) {
strlen($value) > 10;
});
// later
$pipeline = $blueprint->with($iterable);
use Improved\IteratorPipeline\Pipeline;
$unique = Pipeline::build()
->unique()
->values();
$result = $unique($values);
use Improved\IteratorPipeline\Pipeline;
$first = Pipeline::build()->unique()->values();
$second = Pipeline::build()->map(function($value) {
return ucwords($value);
});
$titles = $first->then($second);
$result = $titles($values);
use Improved\IteratorPipeline\Pipeline;
class MyPipeline extends Pipeline
{
function product()
{
$product = 1;
foreach ($this->iterable as $value) {
$product *= $value;
}
return $product;
}
}
$product = (new MyPipeline)->column('amount')->product();
$pipeline = (new Pipeline)->column('amount');
$product = $pipeline
->then(function(iterable $iterable) {
return new MyPipeline($iterable);
})
->product();
$builder = (new PipelineBuilder)
->then(function(iterable $iterable) {
return new MyPipeline($iterable);
})
->column('amount')
->product();
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red'])
->then(function(\Traversable $values): \Generator {
foreach ($values as $key => $value) {
yield $key[0] => "$value $key";
}
})
->toArray(); // ['a' => 'green apple', 'b' => 'blue berry', 'c' => 'red cherry']
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red'])
->then(function(\Traversable $values): \Iterator {
return new MyCustomIterator($values);
});
Pipeline::with(["one", "two", "three"])
->toArray();
Pipeline::with($objects)
->apply(function($object, $key) {
$object->type = $key;
})
->walk();
Pipeline::with([3, 2, 2, 3, 7, 3, 6, 5])
->map(function(int $i): int {
return $i * $i;
})
->toArray(); // [9, 4, 4, 9, 49, 9, 36, 25]
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red'])
->map(function(string $value, string $key): string {
return "{$value} {$key}";
})
->toArray(); // ['apple' => 'green apple', 'berry' => 'blue berry', 'cherry' => 'red cherry']
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red'])
->mapKeys(function(string $value, string $key): string {
return subst($key, 0, 1);
})
->toArray(); // ['a' => 'green', 'b' => 'blue', 'c' => 'red']
$persons = [
'client' => new Person("Max", 18),
'seller' => new Person("Peter", 23),
'lawyer' => new Person("Pamela", 23)
];
Pipeline::with($persons)
->apply(function(Person $value, string $key): void {
$value->role = $key;
})
->toArray();
Pipeline::with(['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'])
->chunk(4);
// <iterator>[
// <iterator>['I', 'II', 'III', 'IV'],
// <iterator>['V', 'VI', 'VII', 'VIII'],
// <iterator>['IX', 'X']
// ]
Pipeline::with(['apple', 'berry', 'cherry', 'apricot'])
->group(function(string $value): string {
return $value[0];
})
->toArray();
// ['a' => ['apple', 'apricot'], 'b' => ['berry'], 'c' => ['cherry']]
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red', 'apricot' => 'orange'])
->group(function(string $value, string $key): string {
return $key[0];
})
->toArray();
// ['a' => ['apple' => 'green', 'apricot' => 'orange'], 'b' => ['berry' => 'blue'], 'c' => ['cherry' => 'red']]
$groups = [
['one', 'two'],
['three', 'four', 'five'],
[],
['six'],
'seven'
];
Pipeline::with($groups)
->flatten()
->toArray(); // ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
$elements = [
['ref' => 'a', 'numbers' => ['I' => 'one', 'II' => 'two']],
['ref' => 'b', 'numbers' => 'three'],
['ref' => 'c', 'numbers' => []]
];
Pipeline::with($elements)
->unwind('numbers')
->toArray();
// [
// ['ref' => 'a', 'numbers' => 'one'],
// ['ref' => 'a', 'numbers' => 'two'],
// ['ref' => 'b', 'numbers' => 'three'],
// ['ref' => 'c', 'numbers' => null]
// ]
Pipeline::with($elements)
->unwind('numbers', 'nrkey')
->toArray();
// [
// ['ref' => 'a', 'numbers' => 'one', 'nrkey' => 'I'],
// ['ref' => 'a', 'numbers' => 'two', 'nrkey' => 'II],
// ['ref' => 'b', 'numbers' => 'three', 'nrkey' => null],
// ['ref' => 'c', 'numbers' => null, 'nrkey' => null]
// ]
$fields = ['foo', 'bar', 'qux'];
Pipeline::with($fields)
->flip()
->fill(42)
->toArray(); // ['foo' => 42, 'bar' => 42, 'qux' => 42]
$rows = [
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'],
['one' => 'yi', 'two' => 'er', 'three' => 'san', 'four' => 'si', 'five' => 'wu'],
['one' => 'één', 'two' => 'twee', 'three' => 'drie', 'five' => 'vijf']
];
Pipeline::with($rows)
->column('three')
->toArray(); // ['tres', 'san', 'drie']
$rows = [
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'],
['one' => 'yi', 'two' => 'er', 'three' => 'san', 'four' => 'si', 'five' => 'wu'],
['one' => 'één', 'two' => 'twee', 'three' => 'drie', 'five' => 'vijf']
];
Pipeline::with($rows)
->column('three', 'two')
->toArray(); // ['dos' => 'tres', 'er' => 'san', 'twee' -=> 'drie']
$rows = [
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'],
['one' => 'yi', 'two' => 'er', 'three' => 'san', 'four' => 'si', 'five' => 'wu'],
['one' => 'één', 'two' => 'twee', 'three' => 'drie', 'five' => 'vijf']
];
Pipeline::with($rows)
->project(['I' => 'one', 'II' => 'two', 'II' => 'three', 'IV' => 'four'])
->toArray();
// [
// ['I' => 'uno', 'II' => 'dos', 'III' => 'tres', 'IV' => 'cuatro'],
// ['I' => 'yi', 'II' => 'er', 'III' => 'san', 'IV' => 'si'],
// ['I' => 'één', 'II' => 'twee', 'III' => 'drie', 'IV' => null]
// ]
$rows = [
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'],
['three' => 'san', 'two' => 'er', 'five' => 'wu', 'four' => 'si'],
['two' => 'twee', 'four' => 'vier']
];
Pipeline::with($rows)
->reshape(['one' => true, 'two' => false, 'three' => 'III', 'four' => 0])
->toArray();
// [
// ['one' => 'uno', 'five' => 'cinco', 'III' => 'tres', 0 => 'cuatro'],
// ['five' => 'wu', 'III' => 'san', 0 => 'si'],
// [0 => 'vier']
// ];
Pipeline::with(['I' => 'one', 'II' => 'two', 'III' => 'three', 'IV' => 'four'])
->values()
->toArray(); // ['one', 'two', 'three', 'four']
Pipeline::with(['I' => 'one', 'II' => 'two', 'III' => 'three', 'IV' => 'four'])
->keys()
->toArray(); // ['I', 'II', 'III', 'IV']
Pipeline::with(['one', 'two', 'three', 'four'])
->setKeys(['I', 'II', 'III', 'IV'])
->toArray(); // ['I' => 'one', 'II' => 'two', 'III' => 'three', 'IV' => 'four']
Pipeline::with(['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro'])
->flip()
->toArray(); // ['uno' => 'one', 'dos' => 'two', 'tres' => 'three', 'cuatro' => 'four']
Pipeline::with([3, 2, 2, 3, 7, 3, 6, 5])
->filter(function(int $i): bool {
return $i % 2 === 0; // is even
})
->toArray(); // [1 => 2, 2 => 2, 6 => 6]
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red', 'apricot' => 'orange'])
->filter(function(string $value, string $key): bool {
return $key[0] === 'a';
})
->toArray(); // ['apple' => 'green', 'apricot' => 'orange']
Pipeline::with(['one', 'two', null, 'four', null])
->cleanup()
->toArray();
// [0 => 'one', 1 => 'two', 3 => 'four']
Pipeline::with(['foo', 'bar', 'qux', 'foo', 'zoo'])
->unique()
->toArray(); // [0 => 'foo', 1 => 'bar', 2 => qux, 4 => 'zoo']
$persons = [
new Person("Max", 18),
new Person("Peter", 23),
new Person("Pamela", 23)
];
Pipeline::with($persons)
->unique(function(Person $value): int {
return $value->age;
})
->toArray();
// [0 => Person {'name' => "Max", 'age' => 18}, 1 => Person {'name' => "Peter", 'age' => 23}]
Pipeline::with($persons)
->unique(function(Person $value): int {
return hash('sha256', serialize($value));
});
});
Pipeline::with(['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red', 'apricot' => 'orange'])
->unique(function(string $value, string $key): string {
return $key[0];
})
->toArray(); // ['apple' => 'green', 'berry' => 'blue', 'cherry' => 'red']
$someGenerator = function($max) {
for ($i = 0; $i < $max; $i++) {
$key = substr(md5((string)$i), 0, 1); // char [0-9a-f]
yield $key => $i;
}
};
Pipeline::with($someGenerator(1000))
->uniqueKeys()
->toArray();
// ['c' => 0, 'e' => 3, 'a' => 4, 1 => 6, 8 => 7, 4 => 9, 'd' => 10, 6 => 11 9 => 15 7 => 17,
// 3 => 21, 'b' => 22, 0 => 27, 'f' => 44, 2 => 51, 5 => 91]
Pipeline::with([3, 2, 2, 3, 7, 3, 6, 5])
->limit(3)
->toArray(); // [3, 2, 2]
Pipeline::with([3, 2, 2, 3, 7, 3, 6, 5])
->slice(3)
->toArray(); // [3, 7, 3, 6, 5]
Pipeline::with([3, 2, 2, 3, 7, 3, 6, 5])
->slice(3, 2)
->toArray(); // [3, 7]
Pipeline::with(['apple' => 'green', 'berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange'])
->before(function($value, $key) {
return $value === 'red';
})
->toArray(); // ['apple' => 'green']
Pipeline::with(['apple' => 'green', 'berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange'])
->before(function($value, $key) {
return $key === 'berry';
})
->toArray(); // ['apple' => 'green']
Pipeline::with(['apple' => 'green', 'berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange'])
->before(function($value) {
return $value === 'red';
})
->toArray(); // ['apple' => 'green', 'berry' => 'red']
Pipeline::with(['apple' => 'green', 'berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange'])
->before(function($value, $key) {
return $value === 'red';
})
->toArray(); // ['cherry' => 'red', 'apricot' => 'orange']
Pipeline::with(['apple' => 'green', 'berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange'])
->before(function($value, $key) {
return $key === 'berry';
})
->toArray(); // ['cherry' => 'red', 'apricot' => 'orange']
Pipeline::with(['apple' => 'green', 'berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange'])
->before(function($value) {
return $value === 'red';
})
->toArray(); // ['berry' => 'red', 'cherry' => 'red', 'apricot' => 'orange']
Pipeline::with(["Charlie", "Echo", "Bravo", "Delta", "Foxtrot", "Alpha"])
->sort()
->toArray(); // ["Alpha", "Beta", "Charlie", "Delta", "Echo", "Foxtrot"]
Pipeline::with(["Charlie", "Echo", "Bravo", "Delta", "Foxtrot", "Alpha"])
->sort(function($a, $b): int {
return strlen($a) <=> strlen($b) ?: $a <=> $b;
})
->toArray(); // ["Echo", "Alpha", "Bravo", "Delta", "Charlie", "Foxtrot"]
Pipeline::with(["Charlie" => "three", "Bravo" => "two", "Delta" => "four", "Alpha" => "one"])
->sortKeys()
->toArray();
// ["Alpha" => "one", "Bravo" => "two", "Charlie" => "three", "Delta" => "four"]
Pipeline::with(["Charlie" => "three", "Bravo" => "two", "Delta" => "four", "Alpha" => "one"])
->sortKeys(function($a, $b): int {
return strlen($a) <=> strlen($b) ?: $a <=> $b;
})
->toArray();
// ["Alpha" => "one", "Bravo" => "two", "Delta" => "four", "Charlie" => "three"]
Pipeline::with(range(5, 10))
->reverse()
->toArray(); // [5 => 10, 4 => 9, 3 => 8, 2 => 7, 1 => 6, 0 => 5]
Pipeline::with($values)
->typeCheck(['int', 'float'])
->toArray();
Pipeline::with($values)
->expectType('int', new \UnexpectedValue('Element %2$s should be an integer, %1$s given'))
->toArray();
Pipeline::with(["one", "two", "three"])
->first(); // "one"
Pipeline::with(["one", "two", "three"])
->last(); // "three"
Pipeline::with(["one", "two", "three"])
->find(function(string $value): bool {
return substr($value, 0, 1) === 't';
}); // "two"
Pipeline::with(["one" => "uno", "two" => "dos", "three" => "tres"])
->find(function(string $value, string $key): bool {
return substr($key, 0, 1) === 't';
}); // "dos"
Pipeline::with(["I" => "one", "II" => "two", "III" => "three"])
->find(function(string $value): bool {
return substr($value, 0, 1) === 't';
}); // "II"
Pipeline::with(["one" => "uno", "two" => "dos", "three" => "tres"])
->find(function(string $value, string $key): bool {
return substr($key, 0, 1) === 't';
}); // "two"
Pipeline::with(["one", "two", "three"])
->hasAny(function(string $value): bool {
return substr($value, 0, 1) === 't';
}); // true
Pipeline::with(["one", "two", "three"])
->hasAny(function(string $value): bool {
return substr($value, 0, 1) === 't';
}); // false
Pipeline::with(["one", "two", "three"])
->hasNone(function(string $value): bool {
return substr($value, 0, 1) === 't';
}); // false
Pipeline::with([99.7, 24, -7.2, -337, 122.0]))
->min(); // -337
Pipeline::with([99.7, 24, -7.2, -337, 122.0])
->min(function($a, $b) {
return abs($a) <=> abs($b);
}); // -7.2
Pipeline::with([99.7, 24, -7.2, -337, 122.0]))
->max(); // 122.0
Pipeline::with([99.7, 24, -7.2, -337, 122.0])
->max(function($a, $b) {
return abs($a) <=> abs($b);
}); // -337
Pipeline::with([2, 8, 4, 12]))
->count(); // 4
Pipeline::with([2, 3, 4])
->reduce(function(int $product, int $value): int {
return $product * $value;
}, 1); // 24
Pipeline::with(['I' => 'one, 'II' => 'two', 'III' => 'three'])
->reduce(function(string $list, string $value, string $key): string {
return $list . sprintf("{%s:%s}", $key, $value);
}, ''); // "{I:one}{II:two}{III:three}"
Pipeline::with([2, 8, 4, 12])
->sum(); // 26
Pipeline::with([2, 8, 4, 12]))
->average(); // 6.5
Pipeline::with(["hello", "sweet", "world"])
->concat(" - "); // "hello - sweet - world"
$blueprint = Pipeline::build()
->expectType('string')
->stub('process');
->sort();
// Later
$pipeline = $blueprint
->unstub('process', i\iterable_map, i\function_partial(i\string_convert_case, ___, i\STRING_UPPERCASE)));
use Improved as i;
class UpperIterator extends IteratorIterator
{
public function current()
{
return i\string_case_convert(parent::current(), i\STRING_UPPERCASE);
}
};
class NoSpaceIterator extends IteratorIterator
{
public function current()
{
return i\string_replace(parent::current(), " ", "");
}
};
$data = get_some_data();
$iterator = new NoSpaceIterator(new UpperIterator(new ArrayIterator($data)));
foreach ($iterator as $cleanValue) {
echo $cleanValue;
}
foreach ($data as $value) {
$upperValue = i\string_case_convert($value, i\STRING_UPPERCASE);
$cleanValue = i\string_replace($upper, " ", "");
echo $cleanValue;
}
$upperData = array_map(function($value) {
return i\string_case_convert($value, i\STRING_UPPERCASE);
}, $data);
$cleanData = array_map(function($value) {
return i\string_replace($value, i\STRING_UPPERCASE);
}, $upperData);
foreach ($cleanData as $cleanValue) {
echo $cleanValue;
}
$upperData = [];
$cleanData = [];
foreach ($data as $value) {
$upperData[] = i\string_case_convert($value, i\STRING_UPPERCASE);
}
foreach ($upperData as $upperValue) {
$cleanData[] = i\string_replace($upperValue, i\STRING_UPPERCASE);
}
foreach ($cleanData as $cleanValue) {
echo $cleanValue;
}
function iterable_first_word(iterable $values): Generator
{
foreach ($values as $key => $value) {
$word = i\string_before($value, " ");
yield $key => $word;
}
}
function get_values(iterable $values)
{
if (is_array($values)) {
return array_values($values);
}
foreach ($values as $value) {
yield $value;
}
}
function iterable_first_word(iterable $values): Generator
{
var_dump($values);
foreach ($values as $key => $value) {
yield $key => i\string_before($value, " ");
}
}
$words = iterable_first_word($values);
// Nothing is outputted yet
foreach ($words as $word) { // Now we get the var_dump() as the function is executed till yield
// ...
}
function numbers_to($count) {
for ($i = 1; $i <= $count; $i++) {
yield $i;
}
}
$oneToTen = numbers_to(10);
foreach ($oneToTen as $number) {
echo $number;
}
// The following loop will cause an error to be thrown.
foreach ($oneToTen as $number) {
foo($number);
}