{"id":21874565,"url":"https://github.com/smoren/sequence-php","last_synced_at":"2025-09-13T15:55:53.844Z","repository":{"id":65083829,"uuid":"581785887","full_name":"Smoren/sequence-php","owner":"Smoren","description":"Iterator-based sequences for PHP","archived":false,"fork":false,"pushed_at":"2023-02-24T09:33:00.000Z","size":613,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-11-20T16:23:27.116Z","etag":null,"topics":["iterators","php","python-like","range","sequence"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Smoren.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-24T10:25:55.000Z","updated_at":"2023-04-20T11:30:26.000Z","dependencies_parsed_at":"2024-11-28T08:31:13.268Z","dependency_job_id":null,"html_url":"https://github.com/Smoren/sequence-php","commit_stats":{"total_commits":44,"total_committers":3,"mean_commits":"14.666666666666666","dds":"0.045454545454545414","last_synced_commit":"ae50b6dc838d4bc390dfdad2768ee1c2aa2e03a7"},"previous_names":[],"tags_count":11,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fsequence-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fsequence-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fsequence-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Smoren%2Fsequence-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Smoren","download_url":"https://codeload.github.com/Smoren/sequence-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986928,"owners_count":21194145,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["iterators","php","python-like","range","sequence"],"created_at":"2024-11-28T07:12:44.530Z","updated_at":"2025-04-15T01:24:16.991Z","avatar_url":"https://github.com/Smoren.png","language":"PHP","readme":"# PHP Iterator-based sequences\n\n![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/sequence)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/sequence-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/sequence-php/?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/Smoren/sequence-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/sequence-php?branch=master)\n![Build and test](https://github.com/Smoren/sequence-php/actions/workflows/test_master.yml/badge.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\n![MathPHP Logo](docs/images/sequence-php-logo.png)\n\nPython-like sequences with iterators for PHP.\n\n## How to install to your project\n```\ncomposer require smoren/sequence\n```\n\n## Quick Reference\n\n### Loops\n| Functionality                         | Description                  | Code Snippet                                      |\n|---------------------------------------|------------------------------|---------------------------------------------------|\n| [`Range-based for`](#Range-based-for) | Python-like range-based loop | `foreach(xrange($start, $size, $step) as $value)` |\n\n### Sequences\n| Class                                  | Description                     | Code Snippet                                                                |\n|----------------------------------------|---------------------------------|-----------------------------------------------------------------------------|\n| [`Range`](#Range)                      | Iterable arithmetic progression | `new Range($start, $size, $step)`                                           |\n| [`Exponential`](#Exponential)          | Iterable geometric progression  | `new Exponential($start, $size, $step)`                                     |\n| [`DynamicSequence`](#Dynamic-Sequence) | Callback-configurable sequence  | `new DynamicSequence($start, $size, $nextValueGetter, $indexedValueGetter)` |\n\n### Data containers\n| Class                            | Description              | Code Snippet               |\n|----------------------------------|--------------------------|----------------------------|\n| [`IndexedArray`](#Indexed-Array) | Python-like indexed list | `new IndexedArray($array)` |\n\n### Functions\n| Function            | Description                                                              | Code Snippet                                   |\n|---------------------|--------------------------------------------------------------------------|------------------------------------------------|\n| [`xrange`](#xrange) | Creates iterable range                                                   | `xrange($start, $size, $step)`                 |\n| [`map`](#map)       | Maps iterable collections and returns IndexedArray of mapped values      | `map($mapper, ...$collections)`                |\n| [`filter`](#filter) | Filters iterable collection and returning IndexedArray of filtered items | `filter($collection, $filter)`                 |\n| [`reduce`](#reduce) | Reduces an iterable collection                                           | `reduce($collection, $reducer, $initialValue)` |\n\n## Usage\n\n### Loops\n\n#### Range-based for\n\nUnlike the PHP built-in function `range()`, `xrange()` does not create an array, but a `Traversable` object \nthat takes up a small amount of memory, regardless of the number of elements in the sequence.\n\n```php\nuse function Smoren\\Sequence\\Functions\\xrange;\n\nforeach(xrange(5) as $i) { // start: 0; count: 5; step: 1\n    echo \"{$i} \";\n}\n// 0 1 2 3 4\n\nforeach(xrange(1, 5) as $i) { // start: 1; count: 5; step: 1\n    echo \"{$i} \";\n}\n// 1 2 3 4 5\n\nforeach(xrange(1, 5, 2) as $i) { // start: 1; count: 5; step: 2\n    echo \"{$i} \";\n}\n// 1 3 5 7 9\n```\n\n### Sequences\n\n#### Range\n\nIterable arithmetic progression.\n\n```new Range(int|float $start, ?int $size, int|float $step)```\n\nFor infinite sequence use `$size = null`.\n\n```php\nuse Smoren\\Sequence\\Structs\\Range;\nuse Smoren\\Sequence\\Exceptions\\OutOfRangeException;\n\n/* Simple int range */\n$range = new Range(1, 3, 2); // (from, size, step)\nvar_dump($range-\u003eisInfinite()); // false\n\nforeach($range as $value) {\n    echo \"{$value} \";\n}\n// 1 3 5\n\nvar_dump($range[0]); // 1\nvar_dump($range[1]); // 3\nvar_dump($range[2]); // 5\n\ntry {\n    $range[3];\n} catch(OutOfRangeException $e) {\n    echo \"cannot get value from index out of range\\n\";\n}\n\nvar_dump($range[-1]); // 5\nvar_dump($range[-2]); // 3\nvar_dump($range[-3]); // 1\n\ntry {\n    $range[-4];\n} catch(OutOfRangeException $e) {\n    echo \"cannot get value from index out of range\\n\";\n}\n\n/* Infinite int range */\n$range = new Range(1, null, 2);\nvar_dump($range-\u003eisInfinite()); // true\n\nforeach($range as $i =\u003e $value) {\n    echo \"{$value} \";\n    if($i \u003e 100) break;\n}\n// 1 3 5 7 9 11 13...\n\n/* Float range */\n$range = new Range(1.1, 3, 2.1);\nvar_dump($range-\u003eisInfinite()); // false\n\nforeach($range as $value) {\n    echo \"{$value} \";\n}\n// 1.1 3.2 5.3\n```\n\n#### Exponential\n\nIterable geometric progression.\n\n```new Exponential(int|float $start, ?int $size, int|float $step)```\n\nFor infinite sequence use `$size = null`.\n\n```php\nuse Smoren\\Sequence\\Structs\\Exponential;\nuse Smoren\\Sequence\\Exceptions\\OutOfRangeException;\n\n/* Simple int exponential sequence */\n$sequence = new Exponential(1, 4, 2); // (from, size, step)\nvar_dump($sequence-\u003eisInfinite()); // false\n\nforeach($sequence as $value) {\n    echo \"{$value} \";\n}\n// 1 2 4 8\n\nvar_dump($sequence[0]); // 1\nvar_dump($sequence[1]); // 2\nvar_dump($sequence[2]); // 4\nvar_dump($sequence[3]); // 8\n\ntry {\n    $sequence[4];\n} catch(OutOfRangeException $e) {\n    echo \"cannot get value from index out of range\\n\";\n}\n\nvar_dump($sequence[-1]); // 8\nvar_dump($sequence[-2]); // 4\nvar_dump($sequence[-3]); // 2\nvar_dump($sequence[-4]); // 1\n\ntry {\n    $sequence[-5];\n} catch(OutOfRangeException $e) {\n    echo \"cannot get value from index out of range\\n\";\n}\n\n/* Infinite int exponential sequence */\n$sequence = new Exponential(1, null, 2);\nvar_dump($sequence-\u003eisInfinite()); // true\n\nforeach($sequence as $i =\u003e $value) {\n    echo \"{$value} \";\n    if($i \u003e 100) break;\n}\n// 1 2 4 8 16 32 64...\n\n/* Infinite float exponential sequence */\n$sequence = new Exponential(0.5, null, 2);\nvar_dump($sequence-\u003eisInfinite()); // true\n\nforeach($sequence as $value) {\n    echo \"{$value} \";\n}\n// 0.5 0.25 0.125...\n```\n\n#### Dynamic Sequence\n\nImplementation of sequence configured with callables.\n\n```new DynamicSequence(mixed $start, ?int $size, callable $nextValueGetter, ?callable $indexedValueGetter = null)```\n\nFor infinite sequence use `$size = null`.\n\n```php\nuse Smoren\\Sequence\\Structs\\DynamicSequence;\n\n// (from, size, nextValueGetter, indexValueGetter)\n$sequence = new DynamicSequence(1, 5, static function($previousValue) {\n    return $previousValue + 1;\n}, static function($index, $startValue) {\n    return $startValue + $index;\n});\n\nvar_dump(iterator_to_array($sequence));\n// [1, 2, 3, 4, 5]\n```\n\n### Data containers\n\n#### Indexed Array\n\nPython-like indexed list.\n\nIts keys are always an unbroken sequence of natural numbers starting from zero.\n\nIt is also allowed to access array elements from the end with negative indices.\n\nOutOfRangeException will be thrown when trying to access a non-existent index.\n\n```php\nuse Smoren\\Sequence\\Structs\\IndexedArray;\nuse Smoren\\Sequence\\Exceptions\\OutOfRangeException;\n\n$array = new IndexedArray([10, 20, 30]);\n\n$array[0] = 11;\n$array[-1] = 33;\n$array[1] = 22;\nvar_dump(count($array)); // 3\nprint_r($array-\u003etoArray()); // [11, 22, 33]\n\nunset($array[1]);\nprint_r($array-\u003etoArray()); // [11, 33]\n\n$array[] = 111;\nprint_r($array-\u003etoArray()); // [11, 33, 111]\n\ntry {\n    $array[3];\n} catch(OutOfRangeException $e) {\n    echo \"cannot get value from index out of range\\n\";\n}\n\ntry {\n    $array[3] = 1;\n} catch(OutOfRangeException $e) {\n    echo \"cannot set value from index out of range\\n\";\n}\n\ntry {\n    unset($array[3]);\n} catch(OutOfRangeException $e) {\n    echo \"cannot unset value from index out of range\\n\";\n}\n```\n\n### Functions\n\n#### xrange\n\nCreates iterable range.\n\nWorks like `xrange()` function in python2 or `range()` function in python3.\n\n```xrange(int $start, ?int $size = null, int $step = 1): Range```\n\n```php\nuse function Smoren\\Sequence\\Functions\\xrange;\n\n$range = xrange(5);\nprint_r(iterator_to_array($range));\n// [0, 1, 2, 3, 4]\n\n$range = xrange(1, 5);\nprint_r(iterator_to_array($range));\n// [1, 2, 3, 4, 5]\n\n$range = xrange(1, 5, 2);\nprint_r(iterator_to_array($range));\n// [1, 3, 5, 7, 9]\n```\n\n#### map\n\nMaps iterable collection and creating IndexedArray of mapped values as a result.\n\n```map(callable $mapper, iterable ...$collections): IndexedArray```\n\n```php\nuse function Smoren\\Sequence\\Functions\\map;\n\n$ids = [1, 2, 3];\n$names = ['Mary', 'Jane', 'Alice'];\n$result = map(static function(int $id, string $name) {\n    return \"{$id}. {$name}\";\n}, $ids, $names);\nprint_r($result-\u003etoArray());\n// ['1. Mary', '2. Jane', '3. Alice']\n```\n\n#### filter\n\nFilters iterable collection and returning IndexedArray of filtered items.\n\n```filter(iterable $collection, callable $filter): IndexedArray```\n\n```php\nuse function Smoren\\Sequence\\Functions\\filter;\n\n$input = [1, 2, 3, 4, 5];\n$result = filter($input, static function($item) {\n    return $item \u003e 2;\n});\nprint_r($result-\u003etoArray());\n// [3, 4, 5]\n```\n\n#### reduce\n\nReduces an iterable collection.\n\n```reduce(iterable $collection, callable $reducer, mixed $initialValue = null): mixed```\n\n```php\nuse function Smoren\\Sequence\\Functions\\reduce;\n\n$input = [1, 2, 3, 4, 5];\n$result = reduce($input, static function($carry, $item) {\n    return $carry + $item;\n}, 0);\nvar_dump($result);\n// 15\n```\n\n## Unit testing\n```\ncomposer install\ncomposer test-init\ncomposer test\n```\n\n## Standards\n\nPHP Sequence conforms to the following standards:\n\n* PSR-1 — [Basic coding standard](https://www.php-fig.org/psr/psr-1/)\n* PSR-4 — [Autoloader](https://www.php-fig.org/psr/psr-4/)\n* PSR-12 — [Extended coding style guide](https://www.php-fig.org/psr/psr-12/)\n\n\n## License\n\nPHP Sequence is licensed under the MIT License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Fsequence-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmoren%2Fsequence-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmoren%2Fsequence-php/lists"}