Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104859 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37646 invoked from network); 21 Mar 2019 19:23:02 -0000 Received: from unknown (HELO mail-io1-f52.google.com) (209.85.166.52) by pb1.pair.com with SMTP; 21 Mar 2019 19:23:02 -0000 Received: by mail-io1-f52.google.com with SMTP id x3so5674525iol.10 for ; Thu, 21 Mar 2019 09:15:13 -0700 (PDT) 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=xP4nujcLOUAxbyTX8UfBUXnXlGQqKmcMfrOQNHbYytQ=; b=JbnJmWDenk33D6M7L25oIr1uts2Zt/rG1A6YA+lDYnUOiDYGem8rDUMvJzxf9eoMf6 d1OmaOpMY3EMtqyh2I4ve9c0COpzPI9LPcnu5GaGuMZsqdI2IDrCNmedOHKl/UFzPOB0 I6PDBRjpKI3rw+g3glTi8UmuhwpZZpzboZeCzo7nz2+zqpha6nz92fZuza/VqiIaA2cZ xpxpi69jZoKQfewXq5LTty9RFl0c4YZ+8xvDvGfQL9fEe8zkMNHLZ+fzOVy8H7Oqt7ia Tckzc8wqfEav1f299KZSzUNWYgQldGwnHBMZTuG+b1jJV5Ki6laqNILaV5aqMHs2BBij 8Esg== 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=xP4nujcLOUAxbyTX8UfBUXnXlGQqKmcMfrOQNHbYytQ=; b=nP0bMmDW5kdbAdroZjIQDjS/VLpgeq15iwQ7IbR9L06FpYRu9dvSNNy7LamORa5Pws RncDMid41ICigiHIODELcNQUoRraBOSxxldTDnzJ8VpJt4xytTXPPpWgdpvJDQUD5g/7 mEZFowg2/jhu8TuklR94BH7PY0VvyNK8r+3ZGvWFWdPb1qWTjBsSoWTwZB5K2BF1cScx 3sNXY/3T60HM6f0bBvk/Hub+TEh26txOTYksUbdzkRddkmZec1dop1S9vV0XnxSKLQi3 pVCerV+QNUXu64xVGAlWRPzSp0+4GTeYaoJ6DwlJsfCI0L+Kb14/hux01SFoYYLpXDib bdOQ== X-Gm-Message-State: APjAAAUs25I0TNA3qlOYxrI12XX6iUK5Bs+vY4scVJO50syTfyc5SCgB MYM9+ZLmiIhrATCr9LcsvGmzomugomhIELQLalc= X-Google-Smtp-Source: APXvYqyJOeuvALRWnUGh8De3kWXiyy5vKfx6qFQbrzRzHuP7urVSWzYshD/unXGrVYddecEn66624v1Mqj4pI2IhY9c= X-Received: by 2002:a6b:6d15:: with SMTP id a21mr3031188iod.235.1553184912573; Thu, 21 Mar 2019 09:15:12 -0700 (PDT) MIME-Version: 1.0 References: <85417a73-a391-c9ad-49aa-5b88c65b0900@gmail.com> <5c8ea43d.1c69fb81.e50f1.7d18SMTPIN_ADDED_MISSING@mx.google.com> <0d34b3b1-866e-4bab-9a8f-3c70f1fc9721@www.fastmail.com> <752c83ab-0e6f-d8f5-ff15-e1f75b5d04b5@gmail.com> <301552a9-6f01-3177-0e57-3d9aac54cc0c@gmail.com> <7A288F12-1227-4E78-8F9A-0959432F02D8@gmail.com> In-Reply-To: Date: Thu, 21 Mar 2019 16:15:01 +0000 Message-ID: To: Robert Hickman Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000fc09d505849d0a00" Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: rowan.collins@gmail.com (Rowan Collins) --000000000000fc09d505849d0a00 Content-Type: text/plain; charset="UTF-8" On Thu, 21 Mar 2019 at 15:21, Robert Hickman wrote: > In this case nextIf() would have to be implemented something like: > > function nextif($someCondition) { > foreach($this->iteratorValue as $x) { > if( yield $x; > } > } > } > > iterator_to_array would need an internal loop to pull the values from > the generator. I think it would be more like this: function nextif($someCondition) { do { $this->currentIndex++; $x = $this->items[ $this->currentIndex ]; } while ( ! some comparison of $x and $$someCondition); return $x; } nextIf() wouldn't be a generator, it would be a normal function returning one value. There is a nested loop in that imaginary version, but only because you can't indicate "don't return anything"; given we're in imaginary concept land, we could add that, and all sign of inner loops disappears: function nextif($someCondition) { $this->currentIndex++; $x = $this->items[ $this->currentIndex ]; if ( some comparison of $x and $$someCondition) { return $x; } else { return SKIP_THIS_ITEM; } } > Thus, as far as I can see, the generator implementation > would result in two loops running in lock step, rather than only one > in the eager case. > It's more like a pipeline: there's a single loop pumping data in, and at any time there's a single "current item", which is either being checked against the condition, manipulated in some way, or passed out the other end. Each stage in the pipeline doesn't need to track what "current item" means, it's given an input each time the loop cycles. We write that as "foreach ( $previousStage as $x ) { yield $x; }" because it's a familiar syntax, but really we're just defining the logic for a nextStep($x) function. Regards, -- Rowan Collins [IMSoP] --000000000000fc09d505849d0a00--