Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108767 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 73680 invoked from network); 26 Feb 2020 13:29:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2020 13:29:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6E27C180211 for ; Wed, 26 Feb 2020 03:47:35 -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,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 26 Feb 2020 03:47:35 -0800 (PST) Received: by mail-lf1-f49.google.com with SMTP id n30so1743493lfh.6 for ; Wed, 26 Feb 2020 03:47:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=+dK4P6Oke4XkqSmIj/eveQFT3uTnmM+B1Jfcm/yrVCM=; b=pA/Ln0aLPKEDlM3mWZVQKUadhsKMokxvMDAUdsPUx70fZ7XHT27awOxpdqbFKN6fMb frffZSaQly/DTrfEIFQpW7y38jZ0Z0I5W8qpsQMcMjq5ABMSlbt+fIFq2Y4xqCLQroEY IASkxRlgo1/JZAQHeCPphXvEJs/v15W7Wn6icBeJcYuYt8Yyq5ajZOR/iNP/w44rl/6e CdHvmUeLD45T5GeWuFq6HI1LVEMd3Fd0I2Ao17M7hknUrKyumzDlowPMUdS5h2QQccwx whppi48q0L/MOobrN/Aoi4p9zRLeKZIbnC+jrWikjOzMwu82ajVJNCzmFUCDWr0S+wmV VJSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=+dK4P6Oke4XkqSmIj/eveQFT3uTnmM+B1Jfcm/yrVCM=; b=az+gMzwdale13yI3L19wGkK6uwTphOzNlUpMLoIDA8Erzr5qU8gD5uRN3Xf9E47oS0 XVm1/8yu0wCW3IX39VcGz6JEaAfGMfG06/Ri22NLFAqQX1QPYNw13djtoL6tEC3+QHII Tzy6NTejfFHAM3yK9Ebud9pASvZLrCFM82VRZgr1sXT1gv0y2PJ01keFWkjcCrUzYY0j Sd4gjYXLBj0+VXy90b254v8eQ09za5OH2wcem3sQKGIBYfQZ+MbafLAgJ+IJUvGdsYpH DI7U0Ts069d1hDXQMt/BXfurfgMnzmWiHq2WBN7XWMaCc71IJi4z7sPRZOwYB7zN3mry JCdg== X-Gm-Message-State: ANhLgQ2hFpnE2/XLOnEtRLGN82CVhT18lh3kOUDvLzSo0hQgJTEUKnXN LY8zTaY7sBnFAN1CI3CKbH7WJnxz91fWzICIrdI+Jhr95Ec= X-Google-Smtp-Source: APXvYqzKqXFUyDDZhpSN0uAkWZPmNNnHupsNy1dLPy7NagqeArXDS0/8XuSB7nbSig/iHYVBsMPGG0aOmeHDreo2rmE= X-Received: by 2002:ac2:596d:: with SMTP id h13mr2285468lfp.190.1582717650887; Wed, 26 Feb 2020 03:47:30 -0800 (PST) MIME-Version: 1.0 Date: Wed, 26 Feb 2020 12:47:14 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000005c6982059f792ba9" Subject: Support rewinding of generators From: nikita.ppv@gmail.com (Nikita Popov) --0000000000005c6982059f792ba9 Content-Type: text/plain; charset="UTF-8" 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 --0000000000005c6982059f792ba9--