66![ Build and test] ( https://github.com/Smoren/sequence-php/actions/workflows/test_master.yml/badge.svg )
77[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT )
88
9- Python-like sequences with iterators for PHP
9+ Python-like sequences with iterators for PHP.
1010
11- ### How to install to your project
11+ ## How to install to your project
1212```
1313composer require smoren/sequence
1414```
1515
16- ### Unit testing
17- ```
18- composer install
19- composer test-init
20- composer test
21- ```
16+ ## Quick Reference
17+
18+ ### Loops
19+ | Functionality | Description | Code Snippet |
20+ | ---------------------------------------| ------------------------------| ---------------------------------------------------|
21+ | [ ` Range-based for ` ] ( #Range-based-for ) | Python-like range-based loop | ` foreach(xrange($start, $size, $step) as $value) ` |
22+
23+ ### Sequences
24+ | Class | Description | Code Snippet |
25+ | ----------------------------------------| ---------------------------------| -----------------------------------------------------------------------------|
26+ | [ ` Range ` ] ( #Range ) | Iterable arithmetic progression | ` new Range($start, $size, $step) ` |
27+ | [ ` Exponential ` ] ( #Exponential ) | Iterable geometric progression | ` new Exponential($start, $size, $step) ` |
28+ | [ ` DynamicSequence ` ] ( #Dynamic-Sequence ) | Callback-configurable sequence | ` new DynamicSequence($start, $size, $nextValueGetter, $indexedValueGetter) ` |
29+
30+ ### Data containers
31+ | Class | Description | Code Snippet |
32+ | ----------------------------------| --------------------------| ----------------------------|
33+ | [ ` IndexedArray ` ] ( #Indexed-Array ) | Python-like indexed list | ` new IndexedArray($array) ` |
34+
35+ ### Functions
36+ | Function | Description | Code Snippet |
37+ | ---------------------| --------------------------------------------------------------------------| ------------------------------------------------|
38+ | [ ` xrange ` ] ( #xrange ) | Creates iterable range | ` xrange($start, $size, $step) ` |
39+ | [ ` map ` ] ( #map ) | Maps iterable collections and returns IndexedArray of mapped values | ` map($mapper, ...$collections) ` |
40+ | [ ` filter ` ] ( #filter ) | Filters iterable collection and returning IndexedArray of filtered items | ` filter($collection, $filter) ` |
41+ | [ ` reduce ` ] ( #reduce ) | Reduces an iterable collection | ` reduce($collection, $reducer, $initialValue) ` |
42+
43+ ## Usage
2244
23- ### Usage
45+ ### Loops
2446
25- #### Range-based for (python-like)
47+ #### Range-based for
2648
27- Unlike the built-in function ` range() ` , ` xrange() ` does not create an array, but a ` Traversable ` object
49+ Unlike the PHP built-in function ` range() ` , ` xrange() ` does not create an array, but a ` Traversable ` object
2850that takes up a small amount of memory, regardless of the number of elements in the sequence.
2951
3052``` php
@@ -46,8 +68,16 @@ foreach(xrange(1, 5, 2) as $i) { // start: 1; count: 5; step: 2
4668// 1 3 5 7 9
4769```
4870
71+ ### Sequences
72+
4973#### Range
5074
75+ Iterable arithmetic progression.
76+
77+ ``` new Range(int|float $start, ?int $size, int|float $step) ```
78+
79+ For infinite sequence use ` $size = null ` .
80+
5181``` php
5282use Smoren\Sequence\Structs\Range;
5383use Smoren\Sequence\Exceptions\OutOfRangeException;
@@ -103,6 +133,12 @@ foreach($range as $value) {
103133
104134#### Exponential
105135
136+ Iterable geometric progression.
137+
138+ ``` new Exponential(int|float $start, ?int $size, int|float $step) ```
139+
140+ For infinite sequence use ` $size = null ` .
141+
106142``` php
107143use Smoren\Sequence\Structs\Exponential;
108144use Smoren\Sequence\Exceptions\OutOfRangeException;
@@ -158,7 +194,13 @@ foreach($sequence as $value) {
158194// 0.5 0.25 0.125...
159195```
160196
161- #### DynamicSequence
197+ #### Dynamic Sequence
198+
199+ Implementation of sequence configured with callables.
200+
201+ ``` new DynamicSequence(mixed $start, ?int $size, callable $nextValueGetter, ?callable $indexedValueGetter = null) ```
202+
203+ For infinite sequence use ` $size = null ` .
162204
163205``` php
164206use Smoren\Sequence\Structs\DynamicSequence;
@@ -174,7 +216,17 @@ var_dump(iterator_to_array($sequence));
174216// [1, 2, 3, 4, 5]
175217```
176218
177- #### IndexedArray
219+ ### Data containers
220+
221+ #### Indexed Array
222+
223+ Python-like indexed list.
224+
225+ Its keys are always an unbroken sequence of natural numbers starting from zero.
226+
227+ It is also allowed to access array elements from the end with negative indices.
228+
229+ OutOfRangeException will be thrown when trying to access a non-existent index.
178230
179231``` php
180232use Smoren\Sequence\Structs\IndexedArray;
@@ -213,9 +265,13 @@ try {
213265}
214266```
215267
268+ ### Functions
269+
216270#### xrange
217271
218- Function for creating iterable range.
272+ Creates iterable range.
273+
274+ Works like ` xrange() ` function in python2 or ` range() ` function in python3.
219275
220276``` xrange(int $start, ?int $size = null, int $step = 1): Range ```
221277
@@ -237,24 +293,25 @@ print_r(iterator_to_array($range));
237293
238294#### map
239295
240- Function for mapping iterable collection and creating IndexedArray of mapped values as a result.
296+ Maps iterable collection and creating IndexedArray of mapped values as a result.
241297
242298``` map(iterable $collection, callable $mapper): IndexedArray ```
243299
244300``` php
245301use function Smoren\Sequence\Functions\map;
246302
247- $input = [1, 2, 3, 4, 5];
248- $result = map($input, static function($item) {
249- return $item + 2;
250- });
303+ $ids = [1, 2, 3];
304+ $names = ['Mary', 'Jane', 'Alice'];
305+ $result = map(static function(int $id, string $name) {
306+ return "{$id}. {$name}";
307+ }, $ids, $names);
251308print_r($result->toArray());
252- // [3, 4, 5, 6, 7 ]
309+ // ['1. Mary', '2. Jane', '3. Alice' ]
253310```
254311
255312#### filter
256313
257- Function for filtering iterable collection and returning IndexedArray of filtered items.
314+ Filters iterable collection and returning IndexedArray of filtered items.
258315
259316``` filter(iterable $collection, callable $filter): IndexedArray ```
260317
@@ -271,9 +328,9 @@ print_r($result->toArray());
271328
272329#### reduce
273330
274- Function for reduction of iterable collection.
331+ Reduces an iterable collection.
275332
276- ``` reduce(iterable $collection, callable $reducer, mixed $initialValue = null): IndexedArray ```
333+ ``` reduce(iterable $collection, callable $reducer, mixed $initialValue = null): mixed ```
277334
278335``` php
279336use function Smoren\Sequence\Functions\reduce;
@@ -285,3 +342,14 @@ $result = reduce($input, static function($carry, $item) {
285342var_dump($result);
286343// 15
287344```
345+
346+ ## Unit testing
347+ ```
348+ composer install
349+ composer test-init
350+ composer test
351+ ```
352+
353+ ## License
354+
355+ PHP Sequence is licensed under the MIT License.
0 commit comments