Re: RFC Draft: Comprehensions

From: Date: Wed, 20 Mar 2019 00:35:32 +0000
Subject: Re: RFC Draft: Comprehensions
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-104815@lists.php.net to get a copy of this message
Hi! > Honestly, I cannot think of any case where I'd use a comprehension > where I would definitely want an array and not a generator. In the > majority case both work equally well, cool, but I don't know when I > would even use an array-dependent version. They wouldn't work equally well. Generator is only sequentially-addressable, and array is random-addressable. And in PHP people do a lot of random addressing of arrays. Also, many functions which accept arrays (and array type) won't work with generators. > And converting from a generator to an array is trivial; the other > way, very not so much. Well, doing everything not using this syntax is trivial - if the point is not doing anything that is already trivial, then we don't need this syntax, as iterating over an array/sequence and applying an operation to it is already trivial in both array and generator contexts, it's a two-liner function in most cases. If the point is to add syntax sugar for most frequently used cases, then I claim that the most frequently used case is definitely not the generator one. I don't think I ever used the generator one in python (I use list one all the time), and I maybe used analogous construct in PHP once or twice (and again I use array_map and more complex syntax for array transformations all the time). BTW, how it's not trivial to make generator from array? function generate($array) { foreach($array as $k => $v) yield $k => $v; } Is there something I'm missing here? -- Stas Malyshev smalyshev@gmail.com

« previous php.internals (#104815) next »