Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98573 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14510 invoked from network); 17 Mar 2017 06:40:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Mar 2017 06:40:50 -0000 Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.221 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.221 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.221] ([81.169.146.221:22264] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 68/84-38004-1F48BC85 for ; Fri, 17 Mar 2017 01:40:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1489732846; l=3394; s=domk; d=kelunik.com; h=Content-Type:To:Subject:Date:From:References:In-Reply-To: MIME-Version; bh=pG7eYny37ibTtox+G7EDOgu/Yhx1v0jea1GRuO75ks8=; b=Cs2QGttBmepVbISLyhhrC0O4qhRiB8MH/L0haSvyK2aomPXE6/5ykzXYyuhfhMcLbM UfdtWh5VNhohcTRgk2NulZrENs7iI1DCvNG8Zw8OXPXCN5foXPTxLp80zzDJQ1Kza2L0 Qt7X2QLJcVBVq2IgQHsZvV2iVd0UI604BtSf4= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mls2vWuiu+7SLDup6E67mzuoNJBqD/t9k= X-RZG-CLASS-ID: mo00 Received: from mail-qt0-f177.google.com ([209.85.216.177]) by smtp.strato.de (RZmta 40.1 AUTH) with ESMTPSA id C01bb6t2H6ek6iH (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp384r1 with 384 ECDH bits, eq. 7680 bits RSA)) (Client did not present a certificate) for ; Fri, 17 Mar 2017 07:40:46 +0100 (CET) Received: by mail-qt0-f177.google.com with SMTP id x35so55618422qtc.2 for ; Thu, 16 Mar 2017 23:40:46 -0700 (PDT) X-Gm-Message-State: AFeK/H1StJ38Jp0AqPfpZEYuiU3N12VpFHECLP7BMIHOWMrfyq+21Uspg3glut1GJlRFyKA2BFGonLvxvrXd7w== X-Received: by 10.200.57.1 with SMTP id s1mr11773837qtb.236.1489732845608; Thu, 16 Mar 2017 23:40:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.12.144.167 with HTTP; Thu, 16 Mar 2017 23:40:45 -0700 (PDT) In-Reply-To: References: Date: Fri, 17 Mar 2017 07:40:45 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=001a113a6e3412adc4054ae775bf Subject: Re: [PHP-DEV] Let range() return a generator? From: me@kelunik.com (Niklas Keller) --001a113a6e3412adc4054ae775bf Content-Type: text/plain; charset=UTF-8 > > 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 --001a113a6e3412adc4054ae775bf--