> No mention of yielding keys in the comprehensions. Presume that would
> work?
Yup, that works too. I also included an example in the mail:

    $firstNames = [foreach ($users as $user) yield $user->id =>
$user->firstName];

This creates a map from ids to first names.

> The simplest example I could think of using Memcached
> (assuming libmemcached fetch() does return as soon as it recieves an
> item)
>
> Eg.
>
> function *getMulti(Memcached $memcached, array $keys)
> {
>    if ($memcached->getDelayed($keys)) {
>        while ($item = $memcached->fetch())
>          yield $item['key'] => $item['value'];
>    }
> }
>
> foreach(getMulti($memcached, ['foo', 'bar', ... ]) as $key => $value)
> {
>        doSomeWork();
> }
>
> So doSomeWork would be called as soon as a single value is available,
> rather
> than having to wait til all values have returned.
>
> Should in theory work right?

Yup, that should work :)

Nikita

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to