Re: Let range() return a generator?
| From: | Sara Golemon | Date: | Sat, 18 Mar 2017 01:57:58 +0000 |
| Subject: | Re: Let range() return a generator? | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98582@lists.php.net to get a copy of this message | ||
On Fri, Mar 17, 2017 at 8:40 PM, David Rodrigues <david.proweb@gmail.com> wrote:
> There are some way to PHP be flagged before run some function, and
> understand that you wants to return a Generator instead of array?
> For instance: new Generator(range(1, 1000)) makes range() work like a
> generator instead of return an array - it's an alternative to avoid new
> keywords.
> Or, maybe, some side implementation like Generator::range(1, 1000),
> Generator::array_values($array), etc. (Currently, for instance, we can
> simulate the range() Generator, but not array_values())
>
Why would array_values() as a generator be difficult?
function generate_values(Traversable $in) {
foreach ($in as $k => $v) {
yield $k => $v;
}
}
In general, your
new Generator($arr) concept misses most of the
usefulness of using a generator since arrays are already traversable,
and you've already allocated all the space of a full container.
Where's the benefit from making it a generator?
-Sara