Share
Embed
PHP 5.5 yield and promises
Karel Čížek
August 11, 2013
Transcript
43 slides
-
"The title means exactly what the words say: naked lunch,
a frozen moment when everyone sees what is on the end of every fork." William S. Burroughs, Naked Lunch -
Coroutines generalize subroutines to allow multiple entry points for suspending
and resuming execution at certain locations. -
/* enter */ → function () { return 1; /*
exit */ → } /* enter */ → function () { $i = 0; yield $i++; /* exit */ → /* entrée */ → yield $i++; /* sortie */ → /* entrata */ → yield $i++; /* uscita */ → /* entrada */ → yield $i++; /* salida */ → } -
// program $prog = function () { $i = 0;
while (true) { yield $i++; } }; // interpreter foreach ($prog() as $i) { echo "$i\n"; } -
// program $prog = function () { $user = (yield
fetchUser(1)); $data = (yield fetchData($user->id)); yield [$user, $data]; }; // interpreter Async::flow($prog()); -
// program $prog = function () { for ($id =
0; $id < 9001; $id++) { yield fetchId($id) } }; // interpreter Async::concurrently(100, $prog()); -
// fetch all followers $profile = syncFetchProfile('kaja47'); $ids = [];
$cursor = '-1'; do { $fs = syncFetchFollowers($profile->id, $cursor); $ids = array_merge($ids, $fs->ids); $cursor = $fs->next_cursor_str; } while ($cursor != "0") return [$profile, $ids]; -
// async clusterfuck fetchProfile($userName)->then(function ($profile) { $fetch = function($userId, $cursor)
use (&$fetch) { return fetchFollowers($userId, $cursor)->then(function ($fs) use ($userId, $fetch) { $cursor = $fs->next_cursor_str; if ($cursor == "0") return When::resolve($fs->ids); else return $fetch($userId, $cursor)->then(function($ids) use($fs) { return array_merge($fs->ids, ids); }); }); }; $fetch($profile->id, '-1')->then(function($ids) use($ids) { return [$profile, $ids]; }) }); -
// async $promise = Async::flow(function() { $profile = (yield fetchProfile('kaja47'));
$ids = []; $cursor = '-1'; do { $fs = (yield fetchFollowers($profile->id, $cursor)); $ids = array_merge($ids, $fs->ids); $cursor = $fs->next_cursor_str; } while ($cursor != "0") yield [$profile, $ids]; }); -
// https://github.com/kaja47/flow function flow($f) { $gen = ($f instanceof Generator)
? $f : $f(); $throw = function ($ex) use ($gen) { $gen->throw($ex); }; $fst = true; $recur = function($pureValue) use($gen, &$recur, &$fst) { try { $x = $fst ? $gen->current() : $gen->send($pureValue); } catch (Exception $e) { return When::reject($e); } $fst = false; if (!$gen->valid()) return When::resolve($pureValue); return When::resolve($x)->then($recur, $throw); }; return $recur(null); } -
Async::flow(function () { $conn = (yield connect()); $res = (yield
$conn->query('...'); yield $conn->close(); yield $res; }); -
Async::flow(function () { try { $conn = (yield connect()); }
catch (DatabaseIsDeadException $e) { if (canWePretendLikeItsNotHappening()) { yield fakeData(); } else { throw new OurStartupIsBusted(AND_NOW_FOR_REAL); } } yield $conn->fetchPicturesOfCats(MOAR); }); -
// seq Async::flow(function () { $v1 = (yield p1()); $v2
= (yield p2()); yield f($v1, $v2); }); -
// par Async::flow(function () { list($p1, $p2) = yield When::all([p1(),
p2()]); yield f($p1, $p2); }); -
// par Async::flow(function () { $p1 = p1(); $p1 =
p2(); $v1 = (yield $p1); $v2 = (yield $p2); }); -
// ??? $newObservable = Async::stream(function () use ($observable) { $count
= 0; while (true) { $val = (yield $observable); yield $count += $val->count; } }); -
The only hope now, I felt, was the possibility that
we'd gone to such excess, with our gig, that nobody in a position to bring the hammer down on us could possibly believe it. Hunter S. Thompson, Fear and Loathing in Las Vegas -
This is the end Beautiful friend This is the end
My only friend, the end Of our elaborate plans, the end Of everything that stands, the end No safety or surprise, the end I'll never look into your eyes...again Can you picture what will be So limitless and free Desperately in need...of some...stranger's hand In a...desperate land Lost in a Roman...wilderness of pain And all the children are insane All the children are insane Waiting for the summer rain, yeah -
http://reactphp.org/ https://github.com/reactphp https://github.com/kaja47/flow https://github.com/kaja47/async-mysql https://github.com/kaja47/async-http http://conference.phpnw.org.uk/phpnw12/schedule/igor-wiedler/#video http://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-fu nctional-nodes-biggest-missed-opportunity/ http://blog.jcoglan.com/2013/04/01/callbacks-promises-and-simplicity/ https://gist.github.com/domenic/3889970
http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/ http://brianmckenna.org/blog/category_theory_promisesaplus http://www.dartlang.org/articles/using-future-based-apis/ http://marakana.com/s/post/1453/redemption_from_callback_hell_michael_ja ckson_domenic_denicola_video https://gist.github.com/hellerbarde/2843375