PHP code example of nicofff / lazy-iter
1. Go to this page and download the library: Download nicofff/lazy-iter library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nicofff / lazy-iter example snippets
azyIter\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;
LazyIter::fromArray([2,4,6,8])
->for_each(function(string $n): void{
echo $n;
});