rfc:iteration-tools

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:iteration-tools [2008/10/29 22:08] – reorganized some sections amenthesrfc:iteration-tools [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Request for Comments: Iteration tools in PHP ====== ====== Request for Comments: Iteration tools in PHP ======
   * Version: 1.0   * Version: 1.0
-  * Date: 2008-10-29+  * Date: 2008-11-03
   * Author: Ionut Gabriel Stan <ionut.dot.g.dot.stan.at.gmail.com>   * Author: Ionut Gabriel Stan <ionut.dot.g.dot.stan.at.gmail.com>
-  * Status: RFC not yet completed +  * Status: In the works 
-  * First Published at: http://wiki.php.net/rfc/functional-tools+  * First Published at: http://wiki.php.net/rfc/iteration-tools
  
 This RFC proposes a series of functions or classes This RFC proposes a series of functions or classes
Line 21: Line 21:
     * foreach     * foreach
  
-Depending on the task at hand the processing involved inside these loops may be ridiculously easy or painfully hard. With time, the more you do this the more you realize there's a pattern emerging out there.+Depending on the task at hand the processing involved inside these loops may be ridiculously easy or painfully hard. With time, the more you do this the more you realize there's a pattern emerging out there and there must be some "tools" to ease our job.
  
  
Line 27: Line 27:
 ==== Why do we need tools for iteration ==== ==== Why do we need tools for iteration ====
  
-Given that this is such a recurrent situation and conforming to the DRY principle but also in total respect with common sense an abstraction is required. Thankfully, these patterns were observed almost fourty years ago by some very smart people. These people found out that iterative processes can be abstracted away in a handful of functions.+Given that iteration is such a recurrent situation and conforming to the DRY principlebut also in total respect with common sensean abstraction is required. Thankfully, patterns regarding iteration were observed almost forty years ago by some very smart people. These people found out that iterative processes can be abstracted away in a handful of functions.
 For example: For example:
     * some of the functions modify data in the set     * some of the functions modify data in the set
Line 34: Line 34:
  
 The list may go on with a few other abstracted use cases. The list may go on with a few other abstracted use cases.
-It turns out that separating the iteration from the inner data calculations is a good thing and people came up with what they called higher order functions, that took at least two parameters, the data set to be processed and the *function* that did the processing (which in some of the cases were "unnamed" functions or lambdas). Languages that did not have possibilities for higher order functions made use of their best features and provided different alternatives if any. For example in PHP we have at our disposal the following SPL classes revolving around the same idea:+ 
 +It turns out that separating the iteration from the inner data calculations is a good thing and people came up with some higher order functions, that took at least two parameters, the data set to be processed and the *function* that did the processing (which in some of the cases were "unnamed" functions or lambdas). Languages that did not have possibilities for higher order functions made use of their best features and provided different alternatives if any. For example in PHP we have at our disposal the following SPL classes revolving around the same idea:
     * FilterIterator     * FilterIterator
     * RecursiveFilterIterator     * RecursiveFilterIterator
Line 42: Line 43:
 ====Shortcomings of current tools==== ====Shortcomings of current tools====
 While these classes do their job they have some shortcomings: While these classes do their job they have some shortcomings:
-    * They only iterate over Traversables. Supporting arrays would be nice (Indeed, we have array_map(), array_walk() and some other similar functions for array processing, yet, I think we should unify these similar tools. Not to mention that array_* function do not adhere to some param order standard. +    * They only iterate over Traversables. Supporting arrays would be nice (Indeed, we have array_map(), array_walk() and some other similar functions for array processing, yet, I think we should unify these similar tools. Not to mention that array_* functions do not adhere to some param order standard. 
-    * FilterIterator, RecursiveFilterIterator and SearchIterator require declaration of an aditional classes that extend them +    * FilterIterator, RecursiveFilterIterator and SearchIterator require declaration of an additional classes that extend them
-    * Although not a shortcoming, we need a few more abstraction+
     * CallbackFilterIterator, although I haven't had the chance to use it, it misses one important thing. It's not passing the iterator itself as a third parameter to the callback.     * CallbackFilterIterator, although I haven't had the chance to use it, it misses one important thing. It's not passing the iterator itself as a third parameter to the callback.
  
-What I'm proposing is introducing in the language of the following functions, which are similar to those existing in JavaScript 1.8:+What I'm proposing is introducing in the language of the following functions, which are similar to the Array methods that exist in JavaScript 1.8:
     * map()     * map()
-    * walk() - this is forEach in JavaScript +    * walk() - this is forEach() in JavaScript 
-    * reduce() - an alternative name - in some languages the name is fold() +    * reduce() - in some languages this is fold() 
-    * reduceRight() - an alternative name in some languages the name is foldr()+    * reduceRight() - in some languages this is foldr()
     * filter()     * filter()
     * some()     * some()
Line 58: Line 58:
 ===== Common Misconceptions ===== ===== Common Misconceptions =====
  
-RFCs do not in any way replace discussions on the mailing list.+None that I know of yet.
  
 ===== Proposal and Patch ===== ===== Proposal and Patch =====
 +
  
 Pages from Mozilla Developer Center wiki documenting these kind of functions can be found here: Pages from Mozilla Developer Center wiki documenting these kind of functions can be found here:
Line 70: Line 71:
 ===map()=== ===map()===
     * Signature: array|Iterator map(array|Traversable iter, callback callback)     * Signature: array|Iterator map(array|Traversable iter, callback callback)
-    * Description: runs a function on every item in the array|Traversable and returns the results in an array|Iterator+    * Description: runs a function on every item in the array|Traversable and returns the results into an array or Iterator, depending on the first param type
     * Callback signature: mixed callback(mixed value, mixed key, array|Traversable iter)     * Callback signature: mixed callback(mixed value, mixed key, array|Traversable iter)
- 
  
 ===walk()=== ===walk()===
Line 79: Line 79:
     * Description: runs a function on every item in the array|Traversable and returns nothing (just like a foreach construct with scope)     * Description: runs a function on every item in the array|Traversable and returns nothing (just like a foreach construct with scope)
     * Callback signature: void callback(mixed value, mixed key, array|Traversable iter)     * Callback signature: void callback(mixed value, mixed key, array|Traversable iter)
- 
  
 ===walkRecursive()=== ===walkRecursive()===
     * Should there be such a function?     * Should there be such a function?
- 
  
 ===reduce()=== ===reduce()===
Line 111: Line 109:
  
 Although the above tools were listed as functions, as they don't do that much, they might just as well be class constructors (honestly I don't like this approach). I thought functions could do just fine because of the new namespace support that's why I represented them as such. Although the above tools were listed as functions, as they don't do that much, they might just as well be class constructors (honestly I don't like this approach). I thought functions could do just fine because of the new namespace support that's why I represented them as such.
 +
 +Additionally, one thing I haven't represented in the above signatures is that an additional argument may be passed to the callback function representing an iteration counter.
  
 ==== Use cases ==== ==== Use cases ====
Line 118: Line 118:
 <?php <?php
  
-// 1.1 how could it be done right now -------------------------------------------------+// 1.1 How it could be done right now -------------------------------------------------
 class OnlyPHPFiles extends FilterIterator { class OnlyPHPFiles extends FilterIterator {
     public function accept() {     public function accept() {
Line 131: Line 131:
 } }
  
-// 1.2 or with the CallbackFilterIterator which I don't know with which PHP+// 1.2 Using CallbackFilterIterator which I don't know with which PHP
 // version it will be shipped // version it will be shipped
 $dirs = new CallbackFilterIterator(new DirectoryIterator(__DIR__), function($value) { $dirs = new CallbackFilterIterator(new DirectoryIterator(__DIR__), function($value) {
Line 139: Line 139:
  
  
-// 2.1 how could be done with my proposal --------------------------------------+// 2.1 How it could be done with my proposal --------------------------------------
 $dirs = filter(new DirectoryIterator(__DIR__), function($current, $key, $iterator) { $dirs = filter(new DirectoryIterator(__DIR__), function($current, $key, $iterator) {
     $ext = strtolower(pathinfo($this->getRealPath(), PATHINFO_EXTENSION));     $ext = strtolower(pathinfo($this->getRealPath(), PATHINFO_EXTENSION));
Line 147: Line 147:
 </code> </code>
  
-While the 1.2 example is very similar to 2.1 it differs from it in that it's not passing the iterator to the callback function. Another difference is that CallbackFilterIterator may also be used as a virtual CallbackMapIterator. For example, it's not only filtering the elements of the iterator into a new iterator, but it MAY also change those values. In my proposal, the function that changes values is map() which translates a certain value to another depending on the callback function. filter() only keeps items that validate agains certain criteria inside the callback function.+While the 1.2 example is very similar to 2.1 it differs from it in that it's not passing the iterator to the callback function and, of course, the fact that I use a function instead of an object. Another difference is that present implementation of CallbackFilterIterator (as documented on http://www.php.net/~helly/php/ext/spl/may also be used as a virtual CallbackMapIterator. For example, it's not only filtering the elements of the iterator into a new iterator, but it MAY also change those values. In my proposal, the function that changes values is map() which translates a certain value to another depending on the callback function. filter() only keeps items that validate against certain criteria inside the callback function. I believe a clear distinction between these features must be reflected in the API, thus two different functions in my proposal.
  
-=====Some aditional benefits=====+=====Some additional benefits=====
 As you have seen, my proposal includes a function called walk() which does exactly the same thing as a foreach construct. The real useful thing this function provides is the ability to mimic scope inside a foreach block. So, for example: As you have seen, my proposal includes a function called walk() which does exactly the same thing as a foreach construct. The real useful thing this function provides is the ability to mimic scope inside a foreach block. So, for example:
  
 <code php> <code php>
 foreach ($iter as $elem) { foreach ($iter as $elem) {
-    // everything inside this foreach block +    // everything inside this foreach block is in the global space
-    // is in the global space+
 } }
  
Line 170: Line 169:
 </code> </code>
  
-It wouldn'have been an advantage if our lambdas had been self-executing, just like in JavaScript:+It would have been an advantage if our lambdas had been self-executing, just like in JavaScript, but they aren't:
  
 <code php> <code php>
Line 181: Line 180:
 </code> </code>
  
-==== Rejected Features ====+===== Rejected Features =====
  
-To be completed+None for the moment
  
-==== Similar implementations ====+===== Similar implementations =====
  
 JavaScript 1.8: JavaScript 1.8:
Line 193: Line 192:
 Python: Python:
     * http://www.python.org/doc/2.5.2/tut/node7.html#SECTION007130000000000000000     * http://www.python.org/doc/2.5.2/tut/node7.html#SECTION007130000000000000000
-    * http://www.python.org/doc/2.5.2/lib/itertools-functions.html +    * http://www.python.org/doc/2.5.2/lib/itertools-functions.html (this Python package differs quite much from what I have proposed, nevertheless we may extract other useful things from there too)
- +
- +
-===== Changelog ===== +
- +
- +
  
rfc/iteration-tools.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1