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

SitePoint PHP Blog:
Transducers in PHP Made Easy
Apr 19, 2016 @ 16:16:25

On the SitePoint PHP blog they've posted a tutorial showing you how to work with transducers in PHP. Transducers are pieces of functionality that allow you to transform data in a reusable way.

Have you heard of functional programming, high order functions, etc. before? Probably, right? However, when you hear “transducers”, do you know what those are? [...] A reducing function is just the kind of function you’d pass to reduce – it takes a result so far and a new input and returns the next result-so-far. A transducer is a function that takes one reducing function and returns another.

Transducers were first introduced into Clojure by Rich Hickey, and ported to PHP by Michael Dowling. Transducers are a powerful way to build algorithmic transformations that you can reuse in many contexts. In this article, we’re going to take a look at how they could be useful through a set of practical examples.

They help you get the mtdowling/transducers library installed via Composer and include a simple example using a User instance and uppercasing the first letter of the user's name. Other examples of the transducer functionality are also included such as: converting values to strings, filtering and composing sets of multiple transformations. The tutorial also shows you how to extend the current functionality and create your own transducer class (their example drops null values).

tagged: transducer functional programming reusable tutorial transform

Link: http://www.sitepoint.com/transducers-in-php-explained-and-demonstrated/

Michael Dowling:
Transducers in PHP
Dec 08, 2014 @ 15:28:48

Michael Dowling has a new post to his site announcing a project he's recent released to try to bring some of the functionality of Clojure to PHP with the introduction of transducers.

Rich Hickey recently announced that transducers are going to be added to Clojure, and it prompted a bit brief announcement, Hickey followed up with a couple videos that describe transducers in much more detail: Transducers and Inside Transducers + more.async. Transducers are a very powerful concept that can be utilized in almost any language. In fact, they have been ported to various other languages including JavaScript (2), Python, Ruby, Java, and Go. And now…transducers are available in PHP via transducers.php!

He starts with an official definition of what a transducer is from the Clojure documentation then explains it in a bit more layman's terms as "a fancy way of saying that you can use functions like map and filter on basically any type of data source (not just sequences)" and can output any kind of structure as a result. He then gets into some code examples using his project showing eager and lazy evaluation, how they're composable and a list of the ones the library makes available (and what they do). He then gets into a more complete example of their application with a streams example, working with/modifying a string. He ends the post looking at how to create your own custom transducer and how they compare to generators.

tagged: transducer conjure step data data structure introduction library

Link: http://mtdowling.com/blog/2014/12/04/transducers-php/


Trending Topics: