Nette Documentation Preview

syntax
Funzioni di iteratore
*********************

.[perex]{data-version:4.0.4}
[api:Nette\Utils\Iterables] è una classe statica con funzioni per lavorare con gli iteratori. La sua controparte per gli array è [NetteUtilsArrays |arrays].


Installazione:

```shell
composer require nette/utils
```

Tutti gli esempi presuppongono la creazione di un alias:

```php
use Nette\Utils\Iterables;
```


contains(iterable $iterable, $value): bool .[method]
----------------------------------------------------

Verifica la presenza di un valore nell'iteratore. Utilizza un confronto rigoroso (`===`).

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


containsKey(iterable $iterable, $value): bool .[method]
-------------------------------------------------------

Verifica la presenza di una chiave nell'iteratore. Utilizza un confronto rigoroso (`===`).

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


every(iterable $iterable, callable $predicate): bool .[method]
--------------------------------------------------------------

Verifica se tutti gli elementi dell'iteratore superano un test implementato in `$predicate` con la firma `function ($value, $key, iterable $iterable): bool`.

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

Vedere [some() |#some()].


filter(iterable $iterable, callable $predicate): Generator .[method]
--------------------------------------------------------------------

Iteratore che filtra gli elementi in base a un predicato. Il predicato ha la firma `function ($value, $key, iterable $iterable): bool`. Mantiene le chiavi originali.

```php
$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 .[method]
-------------------------------------------------------------------------------------------

Restituisce il primo elemento (che corrisponde al predicato, se specificato). Se non esiste alcun elemento, restituisce il risultato della chiamata a `$else` o null.
Il parametro `$predicate` ha la firma `function ($value, $key, iterable $iterable): bool`.

```php
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 .[method]
----------------------------------------------------------------------------------------------

Restituisce la chiave del primo elemento (che corrisponde al predicato, se specificato). Se non esiste un elemento, restituisce il risultato della chiamata a `$else` o null. Il predicato ha la firma `function ($value, $key, iterable $iterable): bool`.

```php
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 .[method]
---------------------------------------------------------------

Iteratore che trasforma i valori richiamando `$transformer`. Ha la firma `function ($value, $key, iterable $iterable): bool`. Mantiene le chiavi originali.

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


some(iterable $iterable, callable $predicate): bool .[method]
-------------------------------------------------------------

Verifica se almeno un elemento dell'iteratore supera un test implementato in `$predicate` con la firma `function ($value, $key, iterable $iterable): bool`.

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

Vedere [every() |#every()].

Funzioni di iteratore

Nette\Utils\Iterables è una classe statica con funzioni per lavorare con gli iteratori. La sua controparte per gli array è NetteUtilsArrays.

Installazione:

composer require nette/utils

Tutti gli esempi presuppongono la creazione di un alias:

use Nette\Utils\Iterables;

contains(iterable $iterable, $value)bool

Verifica la presenza di un valore nell'iteratore. Utilizza un confronto rigoroso (===).

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

containsKey(iterable $iterable, $value)bool

Verifica la presenza di una chiave nell'iteratore. Utilizza un confronto rigoroso (===).

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

every(iterable $iterable, callable $predicate)bool

Verifica se tutti gli elementi dell'iteratore superano un test implementato in $predicate con la firma 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

Vedere some().

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

Iteratore che filtra gli elementi in base a un predicato. Il predicato ha la firma function ($value, $key, iterable $iterable): bool. Mantiene le chiavi originali.

$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

Restituisce il primo elemento (che corrisponde al predicato, se specificato). Se non esiste alcun elemento, restituisce il risultato della chiamata a $else o null. Il parametro $predicate ha la firma 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

Restituisce la chiave del primo elemento (che corrisponde al predicato, se specificato). Se non esiste un elemento, restituisce il risultato della chiamata a $else o null. Il predicato ha la firma 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

Iteratore che trasforma i valori richiamando $transformer. Ha la firma function ($value, $key, iterable $iterable): bool. Mantiene le chiavi originali.

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

some(iterable $iterable, callable $predicate)bool

Verifica se almeno un elemento dell'iteratore supera un test implementato in $predicate con la firma 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

Vedere every().