Skip to content

Instantly share code, notes, and snippets.

@thekid
Created April 27, 2016 07:07
Show Gist options
  • Select an option

  • Save thekid/dd01c8a9a7521fc0f26a56ed27736c11 to your computer and use it in GitHub Desktop.

Select an option

Save thekid/dd01c8a9a7521fc0f26a56ed27736c11 to your computer and use it in GitHub Desktop.
Defer processing elements in Sequences
<?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