im0rtality / underscore
Functional programming library for PHP
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ~4.7
This package is not auto-updated.
Last update: 2024-12-21 16:41:21 UTC
README
Functional programming library for PHP
Code example
Library aims to be as easy to use as possible. Here is example of doing some not-so-meaningful operations to show off:
Underscore::from([1,2,3,4,5]) // convert array format ->map(function($num) { return ['number' => $num];}) // filter out odd elements ->filter(function($item) { return ($item['number'] % 2) == 0;}) // vardump elements ->invoke(function($item) { var_dump($item);}) // changed my mind, I only want numbers ->pick('number') // add numbers to 1000 ->reduce(function($sum, $num) { $sum += $num; return $sum; }, 1000) // take result ->value(); // 1006
Motivation
Originaly I needed functional programming magic for other project, so had to pick one lib or write my own.
There is several PHP ports of UnderscoreJS, however none of those fit my requirements (nice code, easy to write, standardized):
- brianhaveri/Underscore.php - not maintained, messy code
- Anahkiasen/underscore-php - Laravel4 package => incompatible with PSR-2
Installation
Via composer:
$ composer require im0rtality/underscore:*
Composer docs recommend to use specific version. You can look them up in Releases.
Documentation
See wiki
Tests
Tests generate coverage reports in clover.xml format
$ vendor/bin/phpunit
License
MIT License: You can do whatever you want as long as you include the original copyright.