Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104821 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 29054 invoked from network); 20 Mar 2019 15:28:32 -0000 Received: from unknown (HELO mail-io1-f49.google.com) (209.85.166.49) by pb1.pair.com with SMTP; 20 Mar 2019 15:28:32 -0000 Received: by mail-io1-f49.google.com with SMTP id b6so1766782iog.0 for ; Wed, 20 Mar 2019 05:20:25 -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; bh=FvpG5axzWdnjyvXK3jisitfkClK+IDauHtxgM9DVxvs=; b=iW4nxj37ypG5BNFWUG88y+BZFTs1MAl2i1HFIbWyEqwq7wT0vxQkcK7rtPT/7whGCG m4ZZSl7GXs00vCPXx+nzal3JTuTDIlJ2AA00+gsCqezzwdUdplTxnrs+aP7oa4pLEnyz WCD7/haPk8iRgD72ub0B+mMoNRJu8uWkmcjIW5YuV8b+O32YE33B4sEKzGnsw1aRlSLW 4qDuKFjNQmKj4vmEcMwnmnkf2Vn8fW9EmimSKpP+2v/G/GxqH2ICcE/sbPPuOLdhhSh9 VEAMfaXy5hpCQckPj3kZiXJRBb+9hc9+FTQSfvvpbDAzQJvYuZNtho2onCkApqyYWNnc mQBg== 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; bh=FvpG5axzWdnjyvXK3jisitfkClK+IDauHtxgM9DVxvs=; b=Wr2XA784WJ7c5TeB5HY3T+3+FEW3fMzuBmU/XxJqOY7l42HeJ6zL4aCvNUHm9UfEpc qSPd/4YvVcosuPLkicCE3ioh5Pw5iICyd1lLQYunyHFlIU5rakaBOA9AftzhowBBCI8i +wwxYKZKb8od4YDRHL//jdueaOcx+xPvsnrzvcQ/o4r6bSIBP65sLAql+bq0C1OVkqoe N+QFgr3HrIC72eQ4RMV0LTsqNIhus8PVj9cgZI0IIsX5yBwLyyJhRfMpbOtAFE7ANY84 QcqjRvLW2cKPtEyXJjeddzZtXSvbT3lljXbbfZ5GZY/d7YKTAGModX9AQnvTCO14XozN grDQ== X-Gm-Message-State: APjAAAUyUiSYew+7Fg87E64W4hq+NQy+Ez7G8293hbMIRTjonrhdu4Un Ablc02Fd5RsvF2PorrgbwEB8BUGTTB8uqeALuF3Ih9DS X-Google-Smtp-Source: APXvYqy+nVrmkIHzFs5w0r9DyrG30q1qS+aNuL1FAut+a0d4RnI/btKpgMX4yVJl8QVram15UMxAh6HxmNRLGLu5DXk= X-Received: by 2002:a6b:6d15:: with SMTP id a21mr4488008iod.235.1553084424556; Wed, 20 Mar 2019 05:20:24 -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> In-Reply-To: Date: Wed, 20 Mar 2019 12:20:13 +0000 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000006e95dd058485a56c" Subject: Re: [PHP-DEV] RFC Draft: Comprehensions From: rowan.collins@gmail.com (Rowan Collins) --0000000000006e95dd058485a56c Content-Type: text/plain; charset="UTF-8" On Wed, 20 Mar 2019 at 00:35, Stanislav Malyshev wrote: > > And converting from a generator to an array is trivial; the other > > way, very not so much. > > BTW, how it's not trivial to make generator from array? > > function generate($array) { foreach($array as $k => $v) yield $k => $v; } > > Is there something I'm missing here? > It's not that you can't make an array into a generator, but you can't make an eagerly-evaluated expression into a lazily-evaluated one. So if you only have array comprehensions, you can use that helper function to write this, but it will allocate memory for an array containing all the matches: $hugeCSVFileIterator = new CSVFileLazyLoader($filename); $filteredCSVFileIterator = generate([ foreach $hugeCSVFileIterator as $line if $line['type'] == 'foo' yield $line['value'] ]); whereas if the comprehension is an iterator, you can do this and preserve the lazy-loading: $hugeCSVFileIterator = new CSVFileLazyLoader($filename); $filteredCSVFileIterator = [ foreach $hugeCSVFileIterator as $line if $line['type'] == 'foo' yield $line['value'] ]; Similarly, an array-only syntax is useless for infinite iterators, whereas an iterator version lets you write a filtered version that's still infinite. I think a syntax that allows for both versions, using something more idiomatically PHPish than different types of brackets, might be sensible. But if we only have one, it should be the iterator version, with a short-hand for iterator_to_array as a separate language improvement. Regards, -- Rowan Collins [IMSoP] --0000000000006e95dd058485a56c--