underscore-php
underscore-php copied to clipboard
Alternative to underscore-php
Frustrated by the lack of updates, we built our own Underscore-like functional programming library since none of the tools out there were maintained, easy to use, or well-documented: https://github.com/nextbigsoundinc/dash
$avgMaleAge = Dash\chain([
['name' => 'John', 'age' => 12, 'gender' => 'male'],
['name' => 'Jane', 'age' => 34, 'gender' => 'female'],
['name' => 'Pete', 'age' => 23, 'gender' => 'male'],
['name' => 'Mark', 'age' => 11, 'gender' => 'male'],
['name' => 'Mary', 'age' => 42, 'gender' => 'female'],
])
->filter(['gender', 'male'])
->map('age')
->average()
->value();
echo "Average male age is $avgMaleAge.";
Highlights:
- Many data types supported: arrays, objects, generators (coming soon),
Traversable
,DirectoryIterator
, and more - Chaining
- Currying
- Lazy evaluation
- Custom operations
- Well-tested: Comprehensive tests with nearly 3,000 test cases and 100% code coverage
I hope it can be useful to someone. Feedback welcome!
Very nice ! If some of you have some other similar alternatives you can maybe list them here and I can add some to the README for other people landing here
The lack of viable alternatives is what prompted us to create our own library. Nonetheless, there are a couple that might be useful depending on your needs:
- maciejczyzewski/bottomline: Relatively well-updated but only seems to work with arrays and not generic iterables
- nikic/iter: Works with any iterable (including generators) but uses composition instead of chaining