rfc:iteration-tools
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:iteration-tools [2008/11/03 14:43] – refining some ideas amenthes | rfc: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 < | * Author: Ionut Gabriel Stan < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 27: | Line 27: | ||
==== Why do we need tools for iteration ==== | ==== Why do we need tools for iteration ==== | ||
- | Given that iteration is such a recurrent situation and conforming to the DRY principle, but also in total respect with common sense, an abstraction is required. Thankfully, patterns regarding iteration were observed almost | + | Given that iteration is such a recurrent situation and conforming to the DRY principle, but also in total respect with common sense, an abstraction is required. Thankfully, patterns regarding iteration were observed almost |
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 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 " | 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 " | ||
* FilterIterator | * FilterIterator | ||
Line 43: | Line 44: | ||
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(), | * They only iterate over Traversables. Supporting arrays would be nice (Indeed, we have array_map(), | ||
- | * FilterIterator, | + | * FilterIterator, |
* CallbackFilterIterator, | * CallbackFilterIterator, | ||
Line 108: | 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, | Additionally, | ||
Line 145: | Line 147: | ||
</ | </ | ||
- | 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:// | + | 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:// |
- | =====Some | + | =====Some |
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: | ||
Line 190: | Line 192: | ||
Python: | Python: | ||
* http:// | * http:// | ||
- | * http:// | + | * http:// |
- | + | ||
- | + | ||
- | ===== Changelog ===== | + | |
- | + | ||
- | + | ||
rfc/iteration-tools.1225723397.txt.gz · Last modified: 2017/09/22 13:28 (external edit)