no22 / sloth
简单的懒加载应用程序迭代器
dev-master
2013-05-04 07:36 UTC
Requires
- php: >=5.2
This package is not auto-updated.
Last update: 2024-09-14 14:24:29 UTC
README
Sloth 是一个适用于 PHP 5.2 或更高版本的简单懒加载应用程序迭代器。
设置
require "Sloth/Autoload.php";
创建迭代器对象
$iter = Sloth::iter($array_or_iterator);
或者
$iter = Sloth::iter($initialValue, $functionCallback);
懒映射
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
无限序列
PHP 5.2 版本
$even = Sloth::iter(0, Sloth::fn('$n + 2')); $evenLessThan10 = $even->takeWhile(Sloth::fn('$n < 10')); foreach ($evenLessThan10 as $e) { echo $e . "\n"; }
PHP 5.3 版本
$even = Sloth::iter(0, function($n){return $n + 2;}); $evenLessThan10 = $even->takeWhile(function($n){return $n < 10;}); foreach ($evenLessThan10 as $e) { echo $e . "\n"; }
许可证
Sloth 采用 MIT 和 GPLv3 双重授权。您可以选择最适合您项目的许可证。