Mapping, Filtering and Reducing in PHP
Over the past couple of years I have transitioned from boilerplate-heavy, imperative code, to begin using the more intuitive
reduce higher-order functions.
In this article I hope to highlight the transformation that occurs, along with simplification, when moving away from the imperative and onto the declarative.
We shall be performing a simple process which transforms a collection of user entries into a filtered collection of their names.
Although trivial in nature, it is a great place to highlight the power of the paradigm shift.
Below you will find the initial collection of user entires we wish to process. The objective is to create a function which returns only the users names, excluding however, the one associated to a supplied id.
Looking at this problem with an imperative mindset leads us to iterating through the collection, adding names to a temporary collection which match the desired predicate. This boilerplate code is sprinkled throughout many code-bases, and although correct, I feel can be improved upon in its intent and expression.
Mapping and Filtering
The first higher-order functions we shall be using to tackle this problem are the
As their names suggests, mapping over a collection applies the given function to each of its’ elements, were as filtering returns the matching elements based on a supplied predicate function.
Using these two functions we are able to break apart the problem very clearly into its two individual pieces.
Unfortunately however, within PHP we do have to provide quiet verbose declarations, but the intent I feel is still more clearly highlighted.
The solution above is a significant improvement in my opinion, but can be made even better with the introduction of
reduce can be considered the backbone of both
filter, as both functions can be created from it.
In this instance the supplied function is applied to each of the elements in the collection.
This may sound similar to a
map, however the addition of an accumulator value (initial value supplied) which is supplied and returned upon each element application, allows you to craft the result you desire.
Using this higher-order function provides you with far more flexibility in the resulting transformation.
Were as mapping focuses on transforming individual elements, and filtering removes elements from the collection, reducing can do both and so much more.
Again sadly PHP’s syntax leaves a lot to be desired in regard to declaration of lambda functions and immutable arrays. However, again the intent of the code to me has been significantly improved from the original imperative solution.