Created
April 27, 2016 07:07
-
-
Save thekid/dd01c8a9a7521fc0f26a56ed27736c11 to your computer and use it in GitHub Desktop.
Defer processing elements in Sequences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace util\data\unittest; | |
| use util\data\Sequence; | |
| class DeferTest extends \unittest\TestCase { | |
| #[@test] | |
| public function defer_using_existing_apis() { | |
| $deferred= []; | |
| $result= Sequence::concat( | |
| Sequence::of([1, 2, 3, 4]) | |
| ->filter(function($i) use(&$deferred) { | |
| if ($i % 2) { | |
| return true; | |
| } else { | |
| $deferred[]= $i; | |
| return false; | |
| } | |
| }), | |
| function() use(&$deferred) { return $deferred; } | |
| ) | |
| ->map(function($i) { return 2 * $i; }) | |
| ; | |
| $this->assertEquals([2, 6, 4, 8], $result->toArray()); | |
| } | |
| #[@test] | |
| public function defer_using_process() { | |
| $result= Sequence::of([1, 2, 3, 4]) | |
| ->process(function($i) { | |
| if ($i % 2) { | |
| return; | |
| } else { | |
| // Defer: Skip now, pass on later | |
| // Retry: Skip now, call process() again | |
| return Processing::defer($i); | |
| } | |
| }) | |
| ->map(function($i) { return 2 * $i; }) | |
| ; | |
| $this->assertEquals([2, 6, 4, 8], $result->toArray()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment