From 9f40674396bf4fb96884872975af5cfb5aba2322 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Fri, 13 Jul 2012 18:00:37 +0200 Subject: [PATCH] Improve functional programming section - Change criteria for filter - Extend the first example to show anonymous function without assignment - Add paragraph on recursion - Wrap to 120 chars --- _posts/03-02-01-Programming-Paradigms.md | 56 ++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index 1208b39..8fbee17 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -5,8 +5,8 @@ isChild: true ## Programming Paradigms PHP is a flexible, dynamic language that supports a variety of programming techniques. It has evolved dramatically over -the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP -5.3 (2009), and traits in PHP 5.4 (2012). +the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP 5.3 +(2009), and traits in PHP 5.4 (2012). ### Object-oriented Programming @@ -18,41 +18,52 @@ interfaces, inheritence, constructors, cloning, exceptions, and more. ### Functional Programming -PHP supports first-class function, meaning that a function itself can be assigned to a variable, both user-defined and built-in. Those -functions referenced by a variable can be invoked dynamically. Functions can be passed as arguments to other functions (feature called -Higher-order functions) and function can return other functions. +PHP supports first-class function, meaning that a function can be assigned to a variable. Both user defined and built-in +functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other +functions (feature called Higher-order functions) and function can return other functions. + +Recursion, a feature that allows a function to call itself is supported by the language, but most of the PHP code focus +on iteration. New anonymous functions (with support for closures) are present since PHP 5.3 (2009). -The most common usage of higher-order functions is when implementing a strategy pattern. Built-in `array_filter` function asks both -for the input array (data) and a function (strategy, callback) used as a filter criteria on each array item. +The most common usage of higher-order functions is when implementing a strategy pattern. Built-in `array_filter` +function asks both for the input array (data) and a function (a strategy or a callback) used as a filter function on +each array item. {% highlight php %} $min + * Creates an anonymous filter function accepting items > $min + * + * Returns a single filter out of a family of "greater than n" filters */ function criteria_greater_than($min) { @@ -63,18 +74,19 @@ function criteria_greater_than($min) $input = array(1, 2, 3, 4, 5, 6); -// Use array_filter on a input with a selected criteria function +// Use array_filter on a input with a selected filter function $output = array_filter($input, criteria_greater_than(3)); print_r($output); // items > 3 {% endhighlight %} -Each criteria function in the family accepts only elements greater than some minimum value. Single criteria returned by `criteria_greater_than` -is a closure whith `$min` argument closed by the value existing in the scope (given as argument when `criteria_greater_than` is called). +Each filter function in the family accepts only elements greater than some minimum value. Single filter returned by +`criteria_greater_than` is a closure whith `$min` argument closed by the value in the scope (given as an argument when +`criteria_greater_than` is called). -Early binding is used by default for importing `$min` variable into the created function. For true closures with late binding one should use -a reference when importing. This can be used with some templating or input validation libraries, where anonymous function is defined to capture -out-of-scope variables and access them later when the anonymous function is evaluated. +Early binding is used by default for importing `$min` variable into the created function. For true closures with late +binding one should use a reference when importing. Imagine a templating or input validation libraries, where closure is +defined to capture variables in scope and access them later when the anonymous function is evaluated. PHP 5.4 added the ability to bind closures to an object's scope and also improved support for callables such that they can be used interchangeably with anonymous functions in almost all cases.