rfc:iterator_chaining

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:iterator_chaining [2021/03/20 20:34] – created maxsemrfc:iterator_chaining [2021/03/21 14:32] (current) maxsem
Line 1: Line 1:
-====== PHP RFC: Your Title Here ======+====== PHP RFC: Iterator chaining ======
   * Version: 0.9   * Version: 0.9
   * Date: 2021-03-20   * Date: 2021-03-20
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-The elevator pitch for the RFC. The first paragraph of this section will be slightly larger to give it emphasisplease write good introduction.+SPL provides a set of iterators that allow feeding one into another to create powerful data pipelines, however doing that is not particularly convenient: 
 + 
 +<code php> 
 +$iterator = new RegexIterator($myIterator, '/some pattern/'); 
 +$iterator = new CallbackFilterIterator($iterator, fn(...) {...}); 
 +$iterator = new LimitIterator($iterator, 0, 123); 
 +$iterator = new InfiniteIterator($iterator); 
 +</code> 
 + 
 +Phew, that'lot of typing! Not easy to comprehend, eitherAlternatively, a functional approach could be used: 
 +<code php> 
 +$iterator = new InfiniteIterator(new LimitIterator(new CallbackFilterIterator(new RegexIterator($myIterator, '/some pattern/'), fn(...) {...}), 0, 123)); 
 +</code> 
 + 
 +Note how the order of writing is gorgeously opposite to the order of data flow. 
 + 
 +Can we make it better?
  
 ===== Proposal ===== ===== Proposal =====
 +I like how this is implemented Rust. Translated to PHP, that would look like:
 +<code php>
 +$iterator = $myIterator->regex('/some pattern/')
 +                       ->callbackFilter(fn(...) {...})
 +                       ->skip(123)
 +                       ->infinite();
 +</code>
 +
 +Wouldn't that be way more readable and easy to write? I'm proposing to implement it the following way:
 +
 +**Create a trait** that extends iterators and allows to feed them into other iterators:
 <code php> <code php>
 trait IteratorChain { trait IteratorChain {
     public function skip(int $count): LimitIterator {}     public function skip(int $count): LimitIterator {}
-    public function regexFilter(string $regex, int $mode = RegexIterator::MATCH, int $flags = 0 , int $preg_flags = 0): RegexIterator {}+    public function limit(int $count): LimitIterator {} 
 +    public function skipAndLimit(int $numToSkip, int $limit): LimitIterator {} 
 +    public function regex(string $regex, int $mode = RegexIterator::MATCH, int $flags = 0 , int $preg_flags = 0): RegexIterator {}
     public function callbackFilter(callable $callback): CallbackIterator {}     public function callbackFilter(callable $callback): CallbackIterator {}
     public function noRewind(): NoRewindIterator {}     public function noRewind(): NoRewindIterator {}
     public function cached(int $flags = CachingIterator::CALL_TOSTRING): CachingIterator {}     public function cached(int $flags = CachingIterator::CALL_TOSTRING): CachingIterator {}
-    public function multiple(int $flags = MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC): MultipleIterator {}+    public function multiple(int $flags = MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC, string|int|null $info = null): MultipleIterator {} 
 +    public function infinite(): InfiniteIterator {}
 } }
 </code> </code>
 +
 +**Use this trait** in all SPL iterators, where appropriate. Full list:
 +  * ''ArrayIterator''
 +  * ''DirectoryIterator''
 +  * ''EmptyIterator'' - for uniformness
 +  * ''IteratorIterator'' and thus all its subclasses
 +  * ''MultipleIterator''
 +  * ''RecursiveArrayIterator''
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-What breaks, and what is the justification for it?+None.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 31: Line 69:
  
 ===== Open Issues ===== ===== Open Issues =====
-Need to  +Need to decide on precise details of interface before going into voting.
- +
-===== Unaffected PHP Functionality ===== +
-List existing areas/features of PHP that will not be changed by the RFC. +
- +
-This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.+
  
 ===== Future Scope ===== ===== Future Scope =====
Line 42: Line 75:
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Include these so readers know where you are heading and can discuss the proposed voting options.+Accept this RFC (yes/no)? - 2/3 votes required
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/iterator_chaining.1616272483.txt.gz · Last modified: 2021/03/20 20:34 by maxsem