Re: List comprehensions and generator expressions for PHP

From: Date: Thu, 28 Jun 2012 17:28:53 +0000
Subject: Re: List comprehensions and generator expressions for PHP
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-61030@lists.php.net to get a copy of this message
> 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

« previous php.internals (#61030) next »