Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Edd Mann:
Mapping, Filtering and Reducing in PHP
Mar 03, 2016 @ 15:28:27

Edd Mann has a post to his site talking about the use of things like "map" and "reduce" in his own development and how to use it in PHP to help reduce data sets and filter them based on certain criteria.

Over the past couple of years I have transitioned from boilerplate-heavy, imperative code, to begin using the more intuitive map, filter and 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.

He starts with a simple array of data: a set of users with their respective IDs and names. He shows a typical approach, using a method that loops through the data to find only the "name" values. He then shows an alternative that makes use of PHP's own array_filter and array_reduce functions to perform the same operation with just a bit more internal handling.

tagged: filter reduce array arrayfilter arrayreduce example simple

Link: http://eddmann.com/posts/mapping-filtering-and-reducing-in-php/

Sebastian Bergmann's Blog:
Map and Reduce in PHP
Feb 20, 2008 @ 15:31:00

In this new blog entry, Sebastian Bergmann talks about two bits of functionality that PHP has that bring it a bit closer to a full functional language - array_map and array_reduce.

Just like Python, PHP is not a full-fledged functional language, but it supports some very useful functional idioms such as map and reduce. A blog posting by Scott Moonen on Functional Python prompted me to write this posting on PHP's array_map() and array_reduce() functions that apply callback functions to arrays.

He includes examples of each (in the functional context) showing how you can map callbacks to the array values, one that adds one to each number in the array and the other that combines the values and returns the sum.

tagged: functional language arraymap arrayreduce example

Link:


Trending Topics: