Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP 5.5 yield and promises

PHP 5.5 yield and promises

Karel Čížek

August 11, 2013
Tweet

More Decks by Karel Čížek

Other Decks in Programming

Transcript

  1. "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
  2. /* 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 */ → }
  3. // program $prog = function () { $i = 0;

    while (true) { yield $i++; } }; // interpreter foreach ($prog() as $i) { echo "$i\n"; }
  4. // program $prog = function () { $user = (yield

    fetchUser(1)); $data = (yield fetchData($user->id)); yield [$user, $data]; }; // interpreter Async::flow($prog());
  5. // program $prog = function () { for ($id =

    0; $id < 9001; $id++) { yield fetchId($id) } }; // interpreter Async::concurrently(100, $prog());
  6. IO

  7. // 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];
  8. // 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]; }) });
  9. // 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]; });
  10. // 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); }
  11. Async::flow(function () { $conn = (yield connect()); $res = (yield

    $conn->query('...'); yield $conn->close(); yield $res; });
  12. 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); });
  13. // seq Async::flow(function () { $v1 = (yield p1()); $v2

    = (yield p2()); yield f($v1, $v2); });
  14. // par Async::flow(function () { $p1 = p1(); $p1 =

    p2(); $v1 = (yield $p1); $v2 = (yield $p2); });
  15. Rx?

  16. // ??? $newObservable = Async::stream(function () use ($observable) { $count

    = 0; while (true) { $val = (yield $observable); yield $count += $val->count; } });
  17. 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
  18. 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
  19. 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