2017 © Pedro Peláez
 

library sloth

simple lazy application iterator

image

no22/sloth

simple lazy application iterator

  • Saturday, May 4, 2013
  • by no22
  • Repository
  • 2 Watchers
  • 1 Stars
  • 57 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Sloth

Sloth is a simple lazy application iterator for PHP 5.2 or later., (*1)

Setup

    require "Sloth/Autoload.php";

Create iterator object

    $iter = Sloth::iter($array_or_iterator);

or, (*2)

    $iter = Sloth::iter($initialValue, $functionCallback);

Lazy map

    function foo($n)    
    {
        echo "foo called.\n";
        return $n * $n;
    }

    $iter = Sloth::iter(array(1,2,3,4,5))->map('foo');

    foreach ($iter as $e) {
        echo $e . "\n";
    }
    foo called.
    1
    foo called.
    4
    foo called.
    9
    foo called.
    16
    foo called.
    25

Infinite sequense

for PHP 5.2, (*3)

    $even = Sloth::iter(0, Sloth::fn('$n + 2'));
    $evenLessThan10 = $even->takeWhile(Sloth::fn('$n < 10'));
    foreach ($evenLessThan10 as $e) {
        echo $e . "\n";
    }

for PHP 5.3, (*4)

    $even = Sloth::iter(0, function($n){return $n + 2;});
    $evenLessThan10 = $even->takeWhile(function($n){return $n < 10;});
    foreach ($evenLessThan10 as $e) {
        echo $e . "\n";
    }

License

Sloth is dual Licensed MIT and GPLv3. You may choose the license that fits best for your project., (*5)

The Versions

04/05 2013

dev-master

9999999-dev

simple lazy application iterator

  Sources   Download

MIT GPL-3.0+

The Requires

  • php >=5.2

 

by Hiroyuki OHARA

iterator