bbujisic / functional
Yet another take on functional primitives for PHP
dev-master
2018-11-21 10:40 UTC
Requires (Dev)
This package is auto-updated.
Last update: 2024-05-21 22:31:18 UTC
README
Nothing special. It just adds a little bit of syntactic sugar for more expressive code.
Example
<?php use bbujisic\functional\Collection; $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $sumSquaresOfOddItems = $collection ->filter(function ($x) { return $x % 2 == 0; }) ->map(function ($x) { return $x * $x; }) ->reduce(function($x, $y) { return $x + $y; });
Comparable imperative-style programming would perform a wee bit better, at a cost of lesser readability:
<?php $collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $sumSquaresOfOddItems = 0; foreach ($collection as $item) { if ($item % 2 == 0) { $sumSquaresOfOddItems += $item * $item; } }