nicofff / lazy-iter
Lazy array function chains inspired in Rust
0.0.3
2024-11-23 22:12 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11
This package is auto-updated.
Last update: 2025-02-23 22:50:13 UTC
README
Lazy array function chains inspired by Rust
Design goals
- Be lazy (as in Lazily evaluated)
- Have a similar interface to Rust's Iterator
- Leverage static code analyzers to validate type correctness
Install
composer require nicofff/lazy-iter
PHP 7.4 required
Examples
<?php require_once __DIR__ . '/vendor/autoload.php'; use LazyIter\LazyIter; use LazyIter\Helpers\Generators\Range; // Calculate the sum of all the squared numbers below a million $sum_squares_under_a_million = (new LazyIter(Range::rangeFrom(1))) // Start with an iterator over all positive numbers ->map(fn($n) => pow($n,2) ) // Square each one of them ->take_while(fn($n) => $n < 1_000_000 ) // Stop once we reach a million ->sum(); // sum them echo $sum_squares_under_a_million;
Project status
Methods implemented (based on Rust's Iterator Trait)
- all
- any
- chain
- collect
- count
- cycle
- filter
- fold
- for_each
- last
- map
- nth
- skip
- sum
- take
- take_while
Type Enforcement
Currently using PHPStan for type validation. Check type_tests
for a list of things it caches for each method
Example
LazyIter::fromArray([2,4,6,8]) ->for_each(function(string $n): void{ echo $n; });
Raises:
Parameter #1 $callable of method
LazyIter\LazyIter<int,int>::for_each() expects callable(int): mixed,
Closure(string): void given.
Accepting contributions to support Phan and Psalm