《PHP 5.5 yield and promises.pdf》由会员分享,可在线阅读,更多相关《PHP 5.5 yield and promises.pdf(43页珍藏版)》请在三一文库上搜索。
1、 yield PHP5.5 K. k47.cz kaja47 Karel ex funkcionalne.cz “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 Lets get right to the heart of this thing. Coroutines generalize subroutines to al
2、low multiple entry points for suspending and resuming execution at certain locations. cooperative tasks, exceptions, event loop, iterators, infinite lists and pipes. /* enter */ function () return 1; /* exit */ /* enter */ function () $i = 0; yield $i+; /* exit */ /* entre */ yield $i+; /* sortie */
3、 /* entrata */ yield $i+; /* uscita */ /* entrada */ yield $i+; /* salida */ Delimited continuation function () / delimitation yield 1; / continuation State machine Programs and interpreters / program $prog = function () $i = 0; while (true) yield $i+; ; / interpreter foreach ($prog() as $i) echo “$
4、in“; / 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 id, $cursor); $ids = array_merge($ids, $fs-ids); $cursor = $fs-next_cursor_str; while ($cursor !
5、= “0“) return $profile, $ids; / async clusterfuck fetchProfile($userName)-then(function ($profile) $fetch = function($userId, $cursor) use ( if ($cursor = “0“) return When:resolve($fs-ids); else return $fetch($userId, $cursor)-then(function($ids) use($fs) return array_merge($fs-ids, ids); ); ); ; $f
6、etch($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_s
7、tr; while ($cursor != “0“) yield $profile, $ids; ); / https:/ function flow($f) $gen = ($f instanceof Generator) ? $f : $f(); $throw = function ($ex) use ($gen) $gen-throw($ex); ; $fst = true; $recur = function($pureValue) use($gen, catch (Exception $e) return When:reject($e); $fst = false; if (!$ge
8、n-valid() return When:resolve($pureValue); return When:resolve($x)-then($recur, $throw); ; return $recur(null); full closure Async:flow(function () $conn = (yield connect(); $res = (yield $conn-query(.); yield $conn-close(); yield $res; ); throw / catch Async:flow(function () try $conn = (yield conn
9、ect(); catch (DatabaseIsDeadException $e) if (canWePretendLikeItsNotHappening() yield fakeData(); else throw new OurStartupIsBusted(AND_NOW_FOR_REAL); yield $conn-fetchPicturesOfCats(MOAR); ); yield is monadic yield is sequential / seq Async:flow(function () $v1 = (yield p1(); $v2 = (yield p2(); yie
10、ld 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); ); yield is pull-based Rx? Promise /.?/ Observable /.*/ / ? $newObservable = Async:stream(funct
11、ion () use ($observable) $count = 0; while (true) $val = (yield $observable); yield $count += $val-count; ); callbacks promises AtroxAsyncHTTP The only hope now, I felt, was the possibility that wed gone to such excess, with our gig, that nobody in a position to bring the hammer down on us could pos
12、sibly believe it. Hunter S. Thompson, Fear and Loathing in Las Vegas Promise($value, $nextPromise) Chain:seq($gen) Chain:concurrently($count, $gen) 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
13、 surprise, the end Ill never look into your eyes.again Can you picture what will be So limitless and free Desperately in need.of some.strangers 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 ht
14、tp:/reactphp.org/ https:/ https:/ https:/ https:/ http:/conference.phpnw.org.uk/phpnw12/schedule/igor-wiedler/#video http:/ nctional-nodes-biggest-missed-opportunity/ http:/ https:/ http:/ http:/brianmckenna.org/blog/category_theory_promisesaplus http:/www.dartlang.org/articles/using-future-based-apis/ http:/ ckson_domenic_denicola_video https:/ will-work-for-foodk47.cz