Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108770 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7716 invoked from network); 26 Feb 2020 17:05:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Feb 2020 17:05:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 627D21804E0 for ; Wed, 26 Feb 2020 07:23:11 -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-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (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 07:23:10 -0800 (PST) Received: by mail-lj1-f178.google.com with SMTP id y6so3582740lji.0 for ; Wed, 26 Feb 2020 07:23:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=E/40EeO5nBtCDAHQ/SPzuV2JnBtLSOXEEkB5AsUjrEU=; b=UuctzbOUB5d1ONwDYhyHK3ZZVOaoycBL1+JTNxSbZHtLctYnBaEY9/1yn7EGvK7zCp xxBM4OjJkvzJ42TfcElERb7Hd9PGmOK9PqlKyEtUTnUibB8VgwSAhDRP5vunoFzP4u7I vEi35g9fuO1402FmzY8YMZ5acTHBNGiOYQnJJ1LeJuXzeQJrPnGc+yzSsX98hb3MAJ1h orfjXaj9ZnVToWsbdsNmK6ToajEgWHfm2UZgaxyN+76xR5tv9pcCO2Vd0SdVPpGKiU1R VRrE/9thzIB1hwwMCU3OY1+JPV65AknJHHORlslWxhRrUPj5obFK1A9Rrv4b9xs5ZPXh BTTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=E/40EeO5nBtCDAHQ/SPzuV2JnBtLSOXEEkB5AsUjrEU=; b=My6GTaYW0hTlcEUiB6i0UEHLQVhGbViJ56ykHfdBNRnFpEwzXSwkaKAEim3aEImR25 Wk4XI+L9zkghKWJ58K+cvojukD6MHmPrj8KLYpz2BPBqrhxye/agIYuDjBRcCU2FksAJ C4+89Hkj0gh0GtBiEZLJ8qUv8xetxGwYzuRnNrzAGukvBey+IV3dVf21LaAXidI+Ij1c NDdJT6XWEgP28zUiy8/vZXTGgEP6p0bZpRMOWKPHs6/rOtVmyOVyXILCpFU4PndUYUG3 BMgfQV0gBPiOQUCYHVvNe3oTzziIKTD4UF3ZGXTJlW/Z/PZOhzAsul8qXImo8qb5NZYj s6oQ== X-Gm-Message-State: APjAAAUl9Q7o48NwIt5h0pd4IvPVzmY8wBZFvjzBFdrTUC1RwRHfdXU6 rKKqra5+OkvS7HezU2Z9iYQnVW7r/5leCrm2AQo= X-Google-Smtp-Source: ADFU+vsBq/z1FIpruv/6ahn6FM5Af7Nxe/bbVFQsmew1k8cFvumKdHA5Uv7s2O1fHdPOLKpyeBug3zpo8x1/QjiYZb4= X-Received: by 2002:a05:651c:cc:: with SMTP id 12mr359106ljr.260.1582730587237; Wed, 26 Feb 2020 07:23:07 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 26 Feb 2020 16:22:51 +0100 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000006d7468059f7c2e6d" Subject: Re: [PHP-DEV] Support rewinding of generators From: nikita.ppv@gmail.com (Nikita Popov) --0000000000006d7468059f7c2e6d Content-Type: text/plain; charset="UTF-8" On Wed, Feb 26, 2020 at 4:03 PM Rowan Tommins wrote: > Hi Nikita, > > On 26 February 2020 11:47:14 GMT+00:00, Nikita Popov > wrote: > >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(). > > This is an interesting idea. > > There is a gotcha though, neatly demonstrated by your example: > > >function map(callable $function, iterable $iterable): \Iterator { > > foreach ($iterable as $key => $value) { > > yield $key => $function($value); > > } > >} > > If the $iterable passed in is anything other than an array, > re-invoking the function won't actually rewind it. Point of order: foreach() always rewinds the array / Iterator it gets. The code does work correctly under the proposed scheme. Nikita --0000000000006d7468059f7c2e6d--