Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98574 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16515 invoked from network); 17 Mar 2017 07:02:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Mar 2017 07:02:56 -0000 Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.65 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.213.65 mail-vk0-f65.google.com Received: from [209.85.213.65] ([209.85.213.65:34829] helo=mail-vk0-f65.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 20/E4-38004-F1A8BC85 for ; Fri, 17 Mar 2017 02:02:55 -0500 Received: by mail-vk0-f65.google.com with SMTP id k62so4747624vkb.2 for ; Fri, 17 Mar 2017 00:02:55 -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=Z9NhJdSMKn6cm2idX6UqH7/tFNLsuPzRy+aumAaMe94=; b=d3gKEq2cNLmPJUqhEdWg1Nt9GGuILntSAxtz7WTFzctNp8ZCHfpWFCqVrIP7RZAFkl gl3+hD1axQhOaB80zyeW/IyMby2o0Uk325fD1Bf3vn+y/JSp5xwy9BAyjq4YVNcDEN6Z AhWxdQw1k1GWzuvZ8tErcekVQyFzgWHNFdU03xwTpCyX6yMwEpBvoGDwLjlFBbbRhEGb BKQcKsu/01ACbJBZ+2Jjz1QqB5VD4iAyArGbgDQHgch1PVVTt07k+3qI6nlXSNbFq52t MxmuOeyWdN7p15lETqomwtM53zFw3HpGCKn/+3qbASzpFwDnvRVVspPGGOSgfQ+RELNL vNfQ== 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=Z9NhJdSMKn6cm2idX6UqH7/tFNLsuPzRy+aumAaMe94=; b=AzjqXn/jszFnAb49BllmT3boL/Rsdkg3o5h2wuiJJX+MnXW6mQGvPs7ZIyR8oMmcQQ w7BO6FQxQk+tUB9Wjhk+LdeSXBR++dMEw/CJIF5gqGatbTwbLuVSpxzjn+n5iEJfPOZJ s+8jQxtzBRHjZN5t/1hmuErEr03OH8dXisYF9hnEwlh9r8WuBwQ2GeVwbjKIpSBZOJue MugSbV4M7TAsvQq82md7IK8IpZYD/0bA7NxRxi/wy8hEGCRKl8KOpdCmy57STeki3qWl GR6fOavHa865rC6bQWg6rri+wH2cELSZej3ap4N2LGt7Eg+6X8V1r7JyUpxiIN3FqHWY 7g8A== X-Gm-Message-State: AFeK/H1f+9Igqx+hHdL6sBxkdNBbMe3G9MBU5yqvK/m9aikxqUrRmN2CYs0/JFISkCAIQr0Ju9xfVUsJT7iC9Q== X-Received: by 10.31.219.131 with SMTP id s125mr5332631vkg.118.1489734171858; Fri, 17 Mar 2017 00:02:51 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.132.204 with HTTP; Fri, 17 Mar 2017 00:02:31 -0700 (PDT) In-Reply-To: References: Date: Fri, 17 Mar 2017 04:02:31 -0300 Message-ID: To: Niklas Keller Cc: PHP Internals Content-Type: multipart/alternative; boundary=94eb2c07be061f9c21054ae7c4a8 Subject: Re: [PHP-DEV] Let range() return a generator? From: david.proweb@gmail.com (David Rodrigues) --94eb2c07be061f9c21054ae7c4a8 Content-Type: text/plain; charset=UTF-8 I guess that a lot of PHP functions that could be replaced by a Generator (basically all array functions like array_values(), array_keys(), etc). But there are a big question: could it be applied to any case? It will be faster than just an array? Certainly it will be more memory optimized. About BC, it really could happen. Currently you could use range() that will returns an array, then if you use functions like is_array() it will fail to range() with that change. One option is mark a Generator acceptable for is_array(), but it generates another BC, and not make much sense. The best option is mark it as PHP 8.x BC, simplifying all returned arrays to a Generator (if it really help on performance). 2017-03-17 3:40 GMT-03:00 Niklas Keller : > > > > For me, > > > > foreach (range($low, $high) as $i) > > > > is easier to understand than > > > > for ($i = 0; $i <= $high; $i++) > > > > By easier to understand I do not only mean that the beginning of the loop > > marked by the foreach/range construct is easier to read. While reading > the > > body of the loop I can be sure that $i is not unexpectedly modified, for > > instance. > > > > Be that as it may, what I would like to propose should be useful for use > > cases other than the one (see above) that triggered me thinking about > this. > > > > range() returns an array that contains all elements requested. Depending > > on the amount of elements, this might not be optimal. > > > > One of the most common usages of generators that I have seen is an > > alternative, userland implementation of the range() function: > > > > function xrange($start, $end, $step = 1) { > > for ($i = $start; $i <= $end; $i += $step) { > > yield $i; > > } > > } > > > > This makes me wonder: would there be a benefit in changing the > > implementation of range() to return a generator instead of an array? And > if > > so, would this constitute a break in backward compatibility? > > > Yes, that's a BC break. Instead of changing it, there could be a new > function. But as it can be simply built in userland, I don't see much > reason to have it in core. > > Regards, Niklas > -- David Rodrigues --94eb2c07be061f9c21054ae7c4a8--