PHPackages                             smoren/sequence - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. smoren/sequence

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

smoren/sequence
===============

Iterator-based sequences

v3.0.2(3y ago)9624MITPHPPHP &gt;=7.4

Since Dec 24Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Smoren/sequence-php)[ Packagist](https://packagist.org/packages/smoren/sequence)[ RSS](/packages/smoren-sequence/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (13)Used By (0)

PHP Iterator-based sequences
============================

[](#php-iterator-based-sequences)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/a756f93aef2e4ec5f69c876e26ca49172f614721d4b9deeefd3bbdc15a9e3947/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736d6f72656e2f73657175656e6365)](https://camo.githubusercontent.com/a756f93aef2e4ec5f69c876e26ca49172f614721d4b9deeefd3bbdc15a9e3947/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736d6f72656e2f73657175656e6365)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f0d9d8ebf28c6e55f52035e3055ec1e8e8373ff8d0571b37f72e1d29195ba46b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f536d6f72656e2f73657175656e63652d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Smoren/sequence-php/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/e051582d86af75b4b4dd113ea0a7a9184331356921d8961145eb1657e5e24292/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f536d6f72656e2f73657175656e63652d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Smoren/sequence-php?branch=master)[![Build and test](https://github.com/Smoren/sequence-php/actions/workflows/test_master.yml/badge.svg)](https://github.com/Smoren/sequence-php/actions/workflows/test_master.yml/badge.svg)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

[![MathPHP Logo](docs/images/sequence-php-logo.png)](docs/images/sequence-php-logo.png)

Python-like sequences with iterators for PHP.

How to install to your project
------------------------------

[](#how-to-install-to-your-project)

```
composer require smoren/sequence

```

Quick Reference
---------------

[](#quick-reference)

### Loops

[](#loops)

FunctionalityDescriptionCode Snippet[`Range-based for`](#Range-based-for)Python-like range-based loop`foreach(xrange($start, $size, $step) as $value)`### Sequences

[](#sequences)

ClassDescriptionCode Snippet[`Range`](#Range)Iterable arithmetic progression`new Range($start, $size, $step)`[`Exponential`](#Exponential)Iterable geometric progression`new Exponential($start, $size, $step)`[`DynamicSequence`](#Dynamic-Sequence)Callback-configurable sequence`new DynamicSequence($start, $size, $nextValueGetter, $indexedValueGetter)`### Data containers

[](#data-containers)

ClassDescriptionCode Snippet[`IndexedArray`](#Indexed-Array)Python-like indexed list`new IndexedArray($array)`### Functions

[](#functions)

FunctionDescriptionCode Snippet[`xrange`](#xrange)Creates iterable range`xrange($start, $size, $step)`[`map`](#map)Maps iterable collections and returns IndexedArray of mapped values`map($mapper, ...$collections)`[`filter`](#filter)Filters iterable collection and returning IndexedArray of filtered items`filter($collection, $filter)`[`reduce`](#reduce)Reduces an iterable collection`reduce($collection, $reducer, $initialValue)`Usage
-----

[](#usage)

### Loops

[](#loops-1)

#### Range-based for

[](#range-based-for)

Unlike the PHP built-in function `range()`, `xrange()` does not create an array, but a `Traversable` object that takes up a small amount of memory, regardless of the number of elements in the sequence.

```
use function Smoren\Sequence\Functions\xrange;

foreach(xrange(5) as $i) { // start: 0; count: 5; step: 1
    echo "{$i} ";
}
// 0 1 2 3 4

foreach(xrange(1, 5) as $i) { // start: 1; count: 5; step: 1
    echo "{$i} ";
}
// 1 2 3 4 5

foreach(xrange(1, 5, 2) as $i) { // start: 1; count: 5; step: 2
    echo "{$i} ";
}
// 1 3 5 7 9
```

### Sequences

[](#sequences-1)

#### Range

[](#range)

Iterable arithmetic progression.

`new Range(int|float $start, ?int $size, int|float $step)`

For infinite sequence use `$size = null`.

```
use Smoren\Sequence\Structs\Range;
use Smoren\Sequence\Exceptions\OutOfRangeException;

/* Simple int range */
$range = new Range(1, 3, 2); // (from, size, step)
var_dump($range->isInfinite()); // false

foreach($range as $value) {
    echo "{$value} ";
}
// 1 3 5

var_dump($range[0]); // 1
var_dump($range[1]); // 3
var_dump($range[2]); // 5

try {
    $range[3];
} catch(OutOfRangeException $e) {
    echo "cannot get value from index out of range\n";
}

var_dump($range[-1]); // 5
var_dump($range[-2]); // 3
var_dump($range[-3]); // 1

try {
    $range[-4];
} catch(OutOfRangeException $e) {
    echo "cannot get value from index out of range\n";
}

/* Infinite int range */
$range = new Range(1, null, 2);
var_dump($range->isInfinite()); // true

foreach($range as $i => $value) {
    echo "{$value} ";
    if($i > 100) break;
}
// 1 3 5 7 9 11 13...

/* Float range */
$range = new Range(1.1, 3, 2.1);
var_dump($range->isInfinite()); // false

foreach($range as $value) {
    echo "{$value} ";
}
// 1.1 3.2 5.3
```

#### Exponential

[](#exponential)

Iterable geometric progression.

`new Exponential(int|float $start, ?int $size, int|float $step)`

For infinite sequence use `$size = null`.

```
use Smoren\Sequence\Structs\Exponential;
use Smoren\Sequence\Exceptions\OutOfRangeException;

/* Simple int exponential sequence */
$sequence = new Exponential(1, 4, 2); // (from, size, step)
var_dump($sequence->isInfinite()); // false

foreach($sequence as $value) {
    echo "{$value} ";
}
// 1 2 4 8

var_dump($sequence[0]); // 1
var_dump($sequence[1]); // 2
var_dump($sequence[2]); // 4
var_dump($sequence[3]); // 8

try {
    $sequence[4];
} catch(OutOfRangeException $e) {
    echo "cannot get value from index out of range\n";
}

var_dump($sequence[-1]); // 8
var_dump($sequence[-2]); // 4
var_dump($sequence[-3]); // 2
var_dump($sequence[-4]); // 1

try {
    $sequence[-5];
} catch(OutOfRangeException $e) {
    echo "cannot get value from index out of range\n";
}

/* Infinite int exponential sequence */
$sequence = new Exponential(1, null, 2);
var_dump($sequence->isInfinite()); // true

foreach($sequence as $i => $value) {
    echo "{$value} ";
    if($i > 100) break;
}
// 1 2 4 8 16 32 64...

/* Infinite float exponential sequence */
$sequence = new Exponential(0.5, null, 2);
var_dump($sequence->isInfinite()); // true

foreach($sequence as $value) {
    echo "{$value} ";
}
// 0.5 0.25 0.125...
```

#### Dynamic Sequence

[](#dynamic-sequence)

Implementation of sequence configured with callables.

`new DynamicSequence(mixed $start, ?int $size, callable $nextValueGetter, ?callable $indexedValueGetter = null)`

For infinite sequence use `$size = null`.

```
use Smoren\Sequence\Structs\DynamicSequence;

// (from, size, nextValueGetter, indexValueGetter)
$sequence = new DynamicSequence(1, 5, static function($previousValue) {
    return $previousValue + 1;
}, static function($index, $startValue) {
    return $startValue + $index;
});

var_dump(iterator_to_array($sequence));
// [1, 2, 3, 4, 5]
```

### Data containers

[](#data-containers-1)

#### Indexed Array

[](#indexed-array)

Python-like indexed list.

Its keys are always an unbroken sequence of natural numbers starting from zero.

It is also allowed to access array elements from the end with negative indices.

OutOfRangeException will be thrown when trying to access a non-existent index.

```
use Smoren\Sequence\Structs\IndexedArray;
use Smoren\Sequence\Exceptions\OutOfRangeException;

$array = new IndexedArray([10, 20, 30]);

$array[0] = 11;
$array[-1] = 33;
$array[1] = 22;
var_dump(count($array)); // 3
print_r($array->toArray()); // [11, 22, 33]

unset($array[1]);
print_r($array->toArray()); // [11, 33]

$array[] = 111;
print_r($array->toArray()); // [11, 33, 111]

try {
    $array[3];
} catch(OutOfRangeException $e) {
    echo "cannot get value from index out of range\n";
}

try {
    $array[3] = 1;
} catch(OutOfRangeException $e) {
    echo "cannot set value from index out of range\n";
}

try {
    unset($array[3]);
} catch(OutOfRangeException $e) {
    echo "cannot unset value from index out of range\n";
}
```

### Functions

[](#functions-1)

#### xrange

[](#xrange)

Creates iterable range.

Works like `xrange()` function in python2 or `range()` function in python3.

`xrange(int $start, ?int $size = null, int $step = 1): Range`

```
use function Smoren\Sequence\Functions\xrange;

$range = xrange(5);
print_r(iterator_to_array($range));
// [0, 1, 2, 3, 4]

$range = xrange(1, 5);
print_r(iterator_to_array($range));
// [1, 2, 3, 4, 5]

$range = xrange(1, 5, 2);
print_r(iterator_to_array($range));
// [1, 3, 5, 7, 9]
```

#### map

[](#map)

Maps iterable collection and creating IndexedArray of mapped values as a result.

`map(callable $mapper, iterable ...$collections): IndexedArray`

```
use function Smoren\Sequence\Functions\map;

$ids = [1, 2, 3];
$names = ['Mary', 'Jane', 'Alice'];
$result = map(static function(int $id, string $name) {
    return "{$id}. {$name}";
}, $ids, $names);
print_r($result->toArray());
// ['1. Mary', '2. Jane', '3. Alice']
```

#### filter

[](#filter)

Filters iterable collection and returning IndexedArray of filtered items.

`filter(iterable $collection, callable $filter): IndexedArray`

```
use function Smoren\Sequence\Functions\filter;

$input = [1, 2, 3, 4, 5];
$result = filter($input, static function($item) {
    return $item > 2;
});
print_r($result->toArray());
// [3, 4, 5]
```

#### reduce

[](#reduce)

Reduces an iterable collection.

`reduce(iterable $collection, callable $reducer, mixed $initialValue = null): mixed`

```
use function Smoren\Sequence\Functions\reduce;

$input = [1, 2, 3, 4, 5];
$result = reduce($input, static function($carry, $item) {
    return $carry + $item;
}, 0);
var_dump($result);
// 15
```

Unit testing
------------

[](#unit-testing)

```
composer install
composer test-init
composer test

```

Standards
---------

[](#standards)

PHP Sequence conforms to the following standards:

- PSR-1 — [Basic coding standard](https://www.php-fig.org/psr/psr-1/)
- PSR-4 — [Autoloader](https://www.php-fig.org/psr/psr-4/)
- PSR-12 — [Extended coding style guide](https://www.php-fig.org/psr/psr-12/)

License
-------

[](#license)

PHP Sequence is licensed under the MIT License.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Recently: every ~14 days

Total

11

Last Release

1178d ago

Major Versions

v1.1.1 → v2.0.02022-12-29

v2.2.0 → v3.0.02023-01-10

PHP version history (2 changes)v1.0.0PHP &gt;=7.4.0

v3.0.1PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/fdc9e8df44cbbc0fc88885a4f1daa2cad755266e73eb0bac15efaf0d47ecbae2?d=identicon)[smoren](/maintainers/smoren)

---

Top Contributors

[![Smoren](https://avatars.githubusercontent.com/u/7403235?v=4)](https://github.com/Smoren "Smoren (42 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

iteratorsphppython-likerangesequenceiteratorrangesequenceexponentialpython-like

###  Code Quality

TestsCodeception

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/smoren-sequence/health.svg)

```
[![Health](https://phpackages.com/badges/smoren-sequence/health.svg)](https://phpackages.com/packages/smoren-sequence)
```

###  Alternatives

[league/period

Time range API for PHP

7335.4M21](/packages/league-period)[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[nikic/iter

Iteration primitives using generators

1.1k5.9M38](/packages/nikic-iter)[loophp/collection

A (memory) friendly, easy, lazy and modular collection class.

745663.8k13](/packages/loophp-collection)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[kartik-v/yii2-date-range

An advanced Yii 2 date range picker input for based on bootstrap-daterangepicker plugin.

894.4M42](/packages/kartik-v-yii2-date-range)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
