rfc:iterator_chaining

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:iterator_chaining [2021/03/20 21:34] maxsemrfc:iterator_chaining [2021/03/21 14:32] (current) maxsem
Line 22: Line 22:
  
 Note how the order of writing is gorgeously opposite to the order of data flow. 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: **Create a trait** that extends iterators and allows to feed them into other iterators:
Line 30: Line 40:
 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 {}
Line 48: Line 60:
  
 ===== 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 57: 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 68: 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.1616276066.txt.gz · Last modified: 2021/03/20 21:34 by maxsem