-
Notifications
You must be signed in to change notification settings - Fork 0
Samples
lasso edited this page Nov 30, 2016
·
4 revisions
If you have not done so already, please have a look at the basics page.
All code samples below will use the same Enumerable class
use Lasso3000\Enumerable;
class MyEnumerable {
use Enumerable;
protected function __each()
{
yield 'FIRST';
yield 'SECOND';
yield 'THIRD';
}
}$myEnum = new MyEnumerable();
// You can use a built-in function
$myEnum->each('var_dump');
// Prints string(5) "FIRST" string(6)\n"SECOND" string(5)\n"THIRD"\n on the console
// or define your own using a closure
$myEnum->each(
function($elem)
{
echo strrev($elem);
}
);
// Prints TSRIFDNOCESDRIHT on the the console$myEnum = new MyEnumerable();
// You can use a built-in function
$enumArray = $myEnum->map('strtolower');
// $enumArray is an EnumerableArray containing all elements lowercased. You can convert
// the EnumerableArray into an ordinary array by using the toArray method
$arr = $enumArray->toArray();
// $arr contains ['first', 'second', 'third']You can read more about the EnumerableArray class on the basics page.
$myEnum = new MyEnumerable();
// Find all elements with fewer than six characters
$enumArray = $myEnum->findAll(function($elem) { return strlen($elem) < 6; });
// Returns an EnumerableArray containing 'FIRST' and 'THIRD'$myEnum = new MyEnumerable();
// Find all elements with more than five characters
$enumArray = $myEnum->reject(function($elem) { return strlen($elem) < 6; });
// Returns an EnumerableArray containing 'SECOND'