Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98583 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79404 invoked from network); 18 Mar 2017 02:10:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2017 02:10:53 -0000 Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.177 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.217.177 mail-ua0-f177.google.com Received: from [209.85.217.177] ([209.85.217.177:35917] helo=mail-ua0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CD/1B-38004-C279CC85 for ; Fri, 17 Mar 2017 21:10:53 -0500 Received: by mail-ua0-f177.google.com with SMTP id 72so53375877uaf.3 for ; Fri, 17 Mar 2017 19:10:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=3WCrSCSTTaPRr7tNb4IHW6pazBzgavrCF11N1C80wmA=; b=rm6sy5zJPvoD/3bnSrUeKjiBG4Ezb0ESBGExUFcLA+Rkf1oiW2OJgUDXtbqETLXaeL vSHaDkJi+8myHGhP25IjyxX7VYzbh4EYzvdwbSnNmt8G9O9SJnjbA8py7PUuWkzohNBm ZVM4WIVLY+Hj+SAHylUMw5HlJCGRAB8P7zRBVwFwXY1xP99DijlBTne3bpuuVo4bSlma Hd3pIhXBSJZ+UgfCT7y96qwD7hsxmY31xs23npy1sPFOOFKoNm8Cxf4/hsA2X6QkWGhP EshIz0oMZtIGYkwGd8pr4Yd7MlrpgL/TMRZqlu7e7plwCMbTHsy8CBiO3XxHyecBnYhc PDBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=3WCrSCSTTaPRr7tNb4IHW6pazBzgavrCF11N1C80wmA=; b=k2ZYZfXME5SFOcDF9vriXHFeSTiDwONksEeY8KNL7JObQaQRnrRdlV5nKHPTxVCSQH EIGoE6FlnoHf0Svktsazq3nqA9C9S1qm7l0Y2+BemwhaKg1jtBPnBQ326qyAPJIP6RnP 8qECxVO+7HewcwronyRNIdtH353cCBw7u84Thy0CxKau2umWkmT7kEq/ChZzfKFrpy3b AW/M1u0sUowRhLrOP6z5fs7RVJU/LRESTgWmpmNt4CYala+yxBDUpYmmNi3W2DSvOfe9 m+hB9gAJ/hUdrvd2Lb1GXR0Beq6r8DaobAWcrKxdYGEJ9RVIc2HQgNSeYIzDN1xyzjDT T1NQ== X-Gm-Message-State: AFeK/H0zyCMPXIQ4sBvCG90hEKkJF8bexBHvKGzgxHZHf53fP2JbM4T87RuncGZA8w9gquctWZmbqaA7hMZ62w== X-Received: by 10.176.83.56 with SMTP id x53mr3167032uax.41.1489803050455; Fri, 17 Mar 2017 19:10:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.132.204 with HTTP; Fri, 17 Mar 2017 19:10:30 -0700 (PDT) In-Reply-To: References: Date: Fri, 17 Mar 2017 23:10:30 -0300 Message-ID: To: Sara Golemon Cc: Niklas Keller , PHP Internals Content-Type: multipart/alternative; boundary=94eb2c1920ac9b98a9054af7cd99 Subject: Re: [PHP-DEV] Let range() return a generator? From: david.proweb@gmail.com (David Rodrigues) --94eb2c1920ac9b98a9054af7cd99 Content-Type: text/plain; charset=UTF-8 Yeah, sorry, it was a bad example -- I just noted after I submit. hehe About "new Generator($arg)" I mean a method to flag the first argument on function, that should identify that it should work differently or default method (without allocate memory). I guess that it is not implemented on PHP core, because until now have no sense, but imagine something like that (I will write in PHP, but read it like a C++): PHP: new Generator(range(1, 1000)) C++ (range function): if (getParentConstructor() === Generator::class) { // Yield each one. } else { // Prepare on memory and return like an array. } In this case, when you call range() inside a Generator() constructor, then the range() (in C++) will acts differently, like a xrange(). 2017-03-17 22:57 GMT-03:00 Sara Golemon : > On Fri, Mar 17, 2017 at 8:40 PM, David Rodrigues > 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 > -- David Rodrigues --94eb2c1920ac9b98a9054af7cd99--