diff --git a/README.md b/README.md index 7d4ed75..410e71e 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,35 @@ $stringsArray = Wrap::stringsArray(['one', ' two ', 'three']) }); ``` +### `IterableValue` + +Object wrapping iterable. + +```php +join(Wrap::iterable($range(400, 440000))) + ->slice(10, 20) + ->filter(function (int $value): bool { + return $value % 2 === 0; + }) + ->map(function (int $value): int { + return $value + 2; + }) + ->each(function (int $value): void { + echo $value . "\n"; + }); +``` + ## Documentation For full methods reference and more examples see [here](./docs/examples.md). diff --git a/docs/examples.md b/docs/examples.md index 86a9d70..2cad8e5 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -472,7 +472,7 @@ echo $words->shuffle()->implode(' ')->toString(); ``` ``` -no there not is try do or do +no do or there not do try is ``` ### ArrayValue::reverse @@ -3327,4 +3327,526 @@ public function __toString(): string; ``` +## IterableValue + +### IterableValue::each + +```php +each(function (string $letter): void { + echo $letter; +}); +``` + +``` +abc +``` + +### IterableValue::filter + +```php +filter(function (int $number): bool { + return $number % 2 === 0; +}); + +var_export($even->toArray()); +``` + +``` +array ( + 0 => 2, + 1 => 4, +) +``` + +### IterableValue::filterEmpty + +```php +filterEmpty(); + +var_export($notEmpty->toArray()); +``` + +``` +array ( + 0 => 'a', + 1 => 'b', + 2 => 'c', +) +``` + +### IterableValue::map + +```php +map(function (string $letter): string { + return 'new ' . $letter; +}); + +var_export($mapped->toArray()); +``` + +``` +array ( + 0 => 'new a', + 1 => 'new b', + 2 => 'new c', +) +``` + +### IterableValue::flatMap + +```php +join($two)->toArray()); +``` + +``` +array ( + 0 => 1, + 1 => 2, + 2 => 3, + 3 => 8, + 4 => 9, + 5 => 10, +) +``` + +### IterableValue::slice + +```php +slice(2, 4)->toArray()); +echo PHP_EOL; + +var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(-1, 1)->toArray()); +echo PHP_EOL; + +var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(0, 3)->toArray()); +echo PHP_EOL; + +var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(5000, 2)->toArray()); +echo PHP_EOL; +``` + +``` +array ( + 0 => 2, + 1 => 3, + 2 => 4, + 3 => 5, +) +array ( + 0 => 0, +) +array ( + 0 => 0, + 1 => 1, + 2 => 2, +) +array ( + 0 => 5000, + 1 => 5001, +) +``` + +### IterableValue::unique + +```php +reduce($summarize, 0); +echo PHP_EOL; + +$list = function(string $list, int $price): string { + return $list . " €{$price},-"; +}; + +echo 'Prices: ' . $prices->reduce($list, ''); +echo PHP_EOL; +``` + +``` +Sum: 200 +Prices: €10,- €20,- €50,- €120,- +``` + +### IterableValue::notEmpty + +```php +first(); +``` + +``` +first: 1 +``` + +### IterableValue::last + +```php +chunk(3)->toArray()); +``` + +``` +array ( + 0 => + array ( + 0 => 1, + 1 => 2, + 2 => 3, + ), + 1 => + array ( + 0 => 4, + 1 => 3, + 2 => 4, + ), + 2 => + array ( + 0 => 5, + 1 => 6, + 2 => 7, + ), + 3 => + array ( + 0 => 8, + 1 => 9, + ), +) +``` + +### IterableValue::flatten + +```php +chunk(3)->toArray()); diff --git a/examples/example/IterableValue-each.php b/examples/example/IterableValue-each.php new file mode 100644 index 0000000..c2b4803 --- /dev/null +++ b/examples/example/IterableValue-each.php @@ -0,0 +1,8 @@ +each(function (string $letter): void { + echo $letter; +}); diff --git a/examples/example/IterableValue-filter.php b/examples/example/IterableValue-filter.php new file mode 100644 index 0000000..ac08246 --- /dev/null +++ b/examples/example/IterableValue-filter.php @@ -0,0 +1,16 @@ +filter(function (int $number): bool { + return $number % 2 === 0; +}); + +var_export($even->toArray()); diff --git a/examples/example/IterableValue-filterEmpty.php b/examples/example/IterableValue-filterEmpty.php new file mode 100644 index 0000000..09be67b --- /dev/null +++ b/examples/example/IterableValue-filterEmpty.php @@ -0,0 +1,8 @@ +filterEmpty(); + +var_export($notEmpty->toArray()); diff --git a/examples/example/IterableValue-first.php b/examples/example/IterableValue-first.php new file mode 100644 index 0000000..83c6639 --- /dev/null +++ b/examples/example/IterableValue-first.php @@ -0,0 +1,13 @@ +first(); diff --git a/examples/example/IterableValue-join.php b/examples/example/IterableValue-join.php new file mode 100644 index 0000000..9ac52fd --- /dev/null +++ b/examples/example/IterableValue-join.php @@ -0,0 +1,14 @@ +join($two)->toArray()); diff --git a/examples/example/IterableValue-map.php b/examples/example/IterableValue-map.php new file mode 100644 index 0000000..2c581ac --- /dev/null +++ b/examples/example/IterableValue-map.php @@ -0,0 +1,10 @@ +map(function (string $letter): string { + return 'new ' . $letter; +}); + +var_export($mapped->toArray()); diff --git a/examples/example/IterableValue-reduce.php b/examples/example/IterableValue-reduce.php new file mode 100644 index 0000000..8ffbc08 --- /dev/null +++ b/examples/example/IterableValue-reduce.php @@ -0,0 +1,19 @@ +reduce($summarize, 0); +echo PHP_EOL; + +$list = function(string $list, int $price): string { + return $list . " €{$price},-"; +}; + +echo 'Prices: ' . $prices->reduce($list, ''); +echo PHP_EOL; diff --git a/examples/example/IterableValue-slice.php b/examples/example/IterableValue-slice.php new file mode 100644 index 0000000..9979cf9 --- /dev/null +++ b/examples/example/IterableValue-slice.php @@ -0,0 +1,21 @@ +slice(2, 4)->toArray()); +echo PHP_EOL; + +var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(-1, 1)->toArray()); +echo PHP_EOL; + +var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(0, 3)->toArray()); +echo PHP_EOL; + +var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(5000, 2)->toArray()); +echo PHP_EOL; diff --git a/make-examples.php b/make-examples.php index 31ed7f3..d599276 100644 --- a/make-examples.php +++ b/make-examples.php @@ -3,6 +3,7 @@ use doc\GW\Value\ReadmeWriter; use GW\Value\ArrayValue; use GW\Value\AssocValue; +use GW\Value\IterableValue; use GW\Value\StringsArray; use GW\Value\StringValue; @@ -13,6 +14,7 @@ AssocValue::class, StringValue::class, StringsArray::class, + IterableValue::class, ]); file_put_contents('docs/examples.md', $markdown->toString());