bcen/yauc

Yet Another Underscore Clone

0.0.8 2013-02-10 02:27 UTC

README

Usage

Chainable:

use Yauc\Underscore as _;

$stooges = array(
    array(
        'name' => 'curl',
        'age' => 25
    ),
    array(
        'name' => 'moe',
        'age' => 21
    ),
    array(
        'name' => 'larry',
        'age' => 23
    )
);

// 'curl is 25'
_::chain($stooges)
    ->map(function($item) { return $item['name'] . ' is ' . $item['age'];})
    ->first()
    ->value();

OOP non-chain mode:

$mapped = _::init($stooges)->map(function($item) { return $item['name'] . ' is ' . $item['age'];});
$first = _::init($mapped)->first();

OOP-static call:

_::first(_::map($this->stooges, function($item) {
    return $item['name'] . ' is ' . $item['age'];
}));

Functional:

_first(_map($stooges, function($item) {
    return $item['name'] . ' is ' . $item['age'];
}));

Hybrid:

_::init($stooges)
    ->makeChain()
    ->filter(...)
    ->map(...)
    ->pluck(...)
    ->disableChain()
    ->first()

Method Delegate

The implementation of map is too slow? or the pluck is not plucking? No problem!

class MyMethodDelegate extends \Yauc\Delegate\UnderscoreMethodDelegate
{
    public static function map($item, $iterator)
    {
        // ...
    }
}

_::setMethodDelegate('MyMethodDelegate');

_::chain($stooges)
    ->map(function($item) { return $item['name'] . ' is ' . $item['age'];})
    ->first()
    ->value();