Lazy array function chains inspired by Rust
- Be lazy (as in Lazily evaluated)
- Have a similar interface to Rust's Iterator
- Leverage static code analyzers to validate type correctness
composer require nicofff/lazy-iter
PHP 7.4 required
<?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;
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
Currently using PHPStan for type validation. Check type_tests
for a list of things it caches for each method
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