Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
80 changed files
with
3,226 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
Функции на итератора | ||
******************** | ||
|
||
.[perex]{data-version:4.0.4} | ||
[api:Nette\Utils\Iterables] е статичен клас с функции за работа с итератори. Неговият аналог за масиви е [Nette\Utils\Arrays |arrays]. | ||
|
||
|
||
Инсталация: | ||
|
||
```shell | ||
composer require nette/utils | ||
``` | ||
|
||
Всички примери предполагат създаването на псевдоним: | ||
|
||
```php | ||
use Nette\Utils\Iterables; | ||
``` | ||
|
||
|
||
contains(iterable $iterable, $value): bool .[method] | ||
---------------------------------------------------- | ||
|
||
Тества за наличие на стойност в итератора. Използва стриктно сравнение (`===`). | ||
|
||
```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] | ||
------------------------------------------------------- | ||
|
||
Тества за наличие на ключ в итератора. Използва стриктно сравнение (`===`). | ||
|
||
```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] | ||
-------------------------------------------------------------- | ||
|
||
Проверява дали всички елементи на итератора са преминали тест, реализиран в `$predicate` със сигнатура `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 | ||
``` | ||
|
||
Вижте [some( |#some()]). | ||
|
||
|
||
filter(iterable $iterable, callable $predicate): Generator .[method] | ||
-------------------------------------------------------------------- | ||
|
||
Итератор, който филтрира елементите в съответствие с предикат. Предикатът има сигнатурата `function ($value, $key, iterable $iterable): bool`. Запазва оригиналните ключове. | ||
|
||
```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] | ||
------------------------------------------------------------------------------------------- | ||
|
||
Връща първия елемент (отговарящ на предиката, ако е зададен). Ако няма такъв елемент, се връща резултатът от извикването на `$else` или null. | ||
Параметърът `$predicate` има сигнатура `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] | ||
---------------------------------------------------------------------------------------------- | ||
|
||
Връща ключа на първия елемент (отговарящ на предиката, ако е зададен). Ако няма такъв елемент, се връща резултатът от извикването на `$else` или null. Предикатът има сигнатура `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] | ||
--------------------------------------------------------------- | ||
|
||
Итератор, който трансформира стойностите чрез извикване на `$transformer`. Той има сигнатура `function ($value, $key, iterable $iterable): bool`. Запазва оригиналните ключове. | ||
|
||
```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] | ||
------------------------------------------------------------- | ||
|
||
Проверява дали поне един елемент от итератора преминава тест, реализиран в `$predicate` със сигнатура `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 | ||
``` | ||
|
||
Вижте [every( |#every()]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.