Iterator Functions

Nette\Utils\Iterables is a static class with functions for working with iterators. Its counterpart for arrays is Nette\Utils\Arrays.

Installation:

composer require nette/utils

All examples assume the creation of an alias:

use Nette\Utils\Iterables;

contains(iterable $iterable, $value)bool

Tests for the presence of a value in the iterator. It uses strict comparison (===).

Iterables::contains(new ArrayIterator([1, 2, 3]), 1);    // true
Iterables::contains(new ArrayIterator([1, 2, 3]), '1');  // false

containsKey(iterable $iterable, $value)bool

Tests for the presence of a key in the iterator. It uses strict comparison (===).

Iterables::containsKey(new ArrayIterator([1, 2, 3]), 0);  // true
Iterables::containsKey(new ArrayIterator([1, 2, 3]), 4);  // false

every(iterable $iterable, callable $predicate)bool

Tests whether all elements of the iterator pass a test implemented in $predicate with the signature function ($value, $key, iterable $iterable): bool.

$iterator = new ArrayIterator([1, 30, 39, 29, 10, 13]);
$isBelowThreshold = fn($value) => $value < 40;
$res = Iterables::every($iterator, $isBelowThreshold); // true

See some().

filter(iterable $iterable, callable $predicate): Generator

Iterator that filters elements according to a predicate. The predicate has the signature function ($value, $key, iterable $iterable): bool. Maintains original keys.

$iterator = new ArrayIterator([1, 2, 3]);
$iterator = Iterables::filter($iterator, fn($v) => $v < 3);
// 1, 2

first(iterable $iterable, ?callable $predicate=null, ?callable $else=null)mixed

Returns the first item (matching the predicate, if specified). If no such item exists, returns the result of calling $else or null. The $predicate parameter has the signature function ($value, $key, iterable $iterable): bool.

Iterables::first(new ArrayIterator([1, 2, 3]));                   // 1
Iterables::first(new ArrayIterator([1, 2, 3]), fn($v) => $v > 2); // 3
Iterables::first(new ArrayIterator([]));                          // null
Iterables::first(new ArrayIterator([]), else: fn() => false);     // false

firstKey(iterable $iterable, ?callable $predicate=null, ?callable $else=null)mixed

Returns the key of the first item (matching the predicate, if specified). If no such item exists, returns the result of calling $else or null. The predicate has the signature function ($value, $key, iterable $iterable): bool.

Iterables::firstKey(new ArrayIterator([1, 2, 3]));                   // 0
Iterables::firstKey(new ArrayIterator([1, 2, 3]), fn($v) => $v > 2); // 2
Iterables::firstKey(new ArrayIterator(['a' => 1, 'b' => 2]));        // 'a'
Iterables::firstKey(new ArrayIterator([]));                          // null

map(iterable $iterable, callable $transformer)array

Iterator that transforms values by calling $transformer. It has the signature function ($value, $key, iterable $iterable): bool. Maintains original keys.

$iterator = new ArrayIterator([1, 2, 3]);
$iterator = Iterables::map($iterator, fn($v) => $v * 2);
// 2, 4, 6

some(iterable $iterable, callable $predicate)bool

Tests whether at least one element of the iterator passes a test implemented in $predicate with the signature function ($value, $key, iterable $iterable): bool.

$iterator = new ArrayIterator([1, 30, 39, 29, 10, 13]);
$isEven = fn($value) => $value % 2 === 0;
$res = Iterables::some($iterator, $isEven); // true

See every().

version: 4.0