rfc:iteration-tools
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
rfc:iteration-tools [2008/11/02 23:15] – clarifications for some of the ideas presented amenthes | rfc:iteration-tools [2008/11/03 14:43] – refining some ideas amenthes | ||
---|---|---|---|
Line 42: | Line 42: | ||
====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(), | + | * They only iterate over Traversables. Supporting arrays would be nice (Indeed, we have array_map(), |
* FilterIterator, | * FilterIterator, | ||
* CallbackFilterIterator, | * CallbackFilterIterator, | ||
Line 70: | Line 70: | ||
===map()=== | ===map()=== | ||
* Signature: array|Iterator map(array|Traversable iter, callback callback) | * Signature: array|Iterator map(array|Traversable iter, callback callback) | ||
- | * Description: | + | * Description: |
* Callback signature: mixed callback(mixed value, mixed key, array|Traversable iter) | * Callback signature: mixed callback(mixed value, mixed key, array|Traversable iter) | ||
Line 108: | Line 108: | ||
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, | ||
==== Use cases ==== | ==== Use cases ==== | ||
Line 115: | Line 116: | ||
<?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 128: | Line 129: | ||
} | } | ||
- | // 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__), | $dirs = new CallbackFilterIterator(new DirectoryIterator(__DIR__), | ||
Line 136: | Line 137: | ||
- | // 2.1 how could be done with my proposal -------------------------------------- | + | // 2.1 How it could be done with my proposal -------------------------------------- |
$dirs = filter(new DirectoryIterator(__DIR__), | $dirs = filter(new DirectoryIterator(__DIR__), | ||
$ext = strtolower(pathinfo($this-> | $ext = strtolower(pathinfo($this-> | ||
Line 144: | Line 145: | ||
</ | </ | ||
- | 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 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 |
=====Some aditional benefits===== | =====Some aditional benefits===== |
rfc/iteration-tools.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1