Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108768 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98314 invoked from network); 26 Feb 2020 16:23:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2020 16:23:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A1AE01804A7 for ; Wed, 26 Feb 2020 06:41:07 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS63949 45.33.0.0/19 X-Spam-Virus: No X-Envelope-From: Received: from s2.servercanyon.com (s2.servercanyon.com [45.33.0.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 26 Feb 2020 06:41:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=judahwright.me; s=default; h=Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Cc:To:Subject:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=om4cP+e/+H85O1EXDEIgpz5iKah/v1p3qmSY9DV/urI=; b=IzjKAQ9QRiZMg4b/QREj6a5Du Zn50lYWwM9sqmxhZXfE92vqkXYRvbC93ySDQpgcbIQfmCtMVIIFwnQgTkKC9fqhTN9xNvnDdg+nhN VRnEFeqqzTGyXZnSAFhGJNReepesl2OjZnxJ5vr33Ks+1j6WU8QrYWDtkSRWmM2ETH2OvuG+hS+J+ l41somRqTZT91N21eUOdE5dvGIDOfCKBaYnvpp5vugcGwdwOUikP82ffno+bA0dPvivQBuwe0GWX1 1W/JcCylGtw41gAJ4UgwLe7CSXC0b3qTZOX8mtfTVSCIwud6RCMIEWGPAJL2udbXbLVReGwfVunNa HG+kERkeQ==; Received: from 24-116-109-194.cpe.sparklight.net ([24.116.109.194]:44084 helo=[192.168.1.89]) by s2.servercanyon.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1j6xs1-00070U-8e; Wed, 26 Feb 2020 08:41:05 -0600 Date: Wed, 26 Feb 2020 07:41:03 -0700 To: Nikita Popov Cc: PHP internals Message-ID: <1582728063.10268.2@judahwright.me> In-Reply-To: References: X-Mailer: geary/3.34.1 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=-4zbaD+xEBX8CXRIXbv6p" X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - s2.servercanyon.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - judahwright.me X-Get-Message-Sender-Via: s2.servercanyon.com: authenticated_id: hello@judahwright.me X-Authenticated-Sender: s2.servercanyon.com: hello@judahwright.me X-Source: X-Source-Args: X-Source-Dir: Subject: Re: [PHP-DEV] Support rewinding of generators From: hello@judahwright.me (Judah Wright) --=-4zbaD+xEBX8CXRIXbv6p Content-Type: text/plain; charset=us-ascii; format=flowed Perhaps an easy userland implementation could be type-hinting a new generator type, to indicate that the generator should be rewindable by simply re-calling the function? // Safe to rewind function fooRange(int $from, int $to): RewindableGenerator { for ($i = $from; $i <= $to; $i++) { yield $i; } } That should be safe to re-call again, but my concern is with generators that modify some external state that should not be called twice. In these cases having a fatal error is a handy feature to prevent re-iterating over things that might cause issues. ~Judah On Wed, Feb 26, 2020 at 12:47 pm, Nikita Popov wrote: > Hi internals, > > Generators currently do not support rewinding -- or rather, only > support it > if the generator is at/before the first yield, in which case > rewinding is a > no-op. > > Generators make it real breeze to implement primitives like > > function map(callable $function, iterable $iterable): \Iterator { > foreach ($iterable as $key => $value) { > yield $key => $function($value); > } > } > > without having to do through the whole Iterator boilerplate. However, > if > you do this, you end up with an iterator that is not rewindable. If > you > want to make map() rewindable, you need to go back to a manual > Iterator > implementation. As iterators in PHP are assumed to be rewindable by > default, this is somewhat annoying. > > There is a relatively simple (at least conceptually) way to make > generators > rewindable: Remember the original arguments of the function, and > basically > "re-invoke" it on rewind(). > > I'm wondering what people think about adding this functionality. I > think > the main argument against it is that not all generators may behave > sensibly > if you re-run their code -- there's probably a reasonable expectation > that > an iterator will return the same sequence of values are rewinding, > something which we cannot guarantee with generators, but also don't > enforce > with normal iterators either. > > Regards, > Nikita --=-4zbaD+xEBX8CXRIXbv6p--