rfc:comprehensions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:comprehensions [2019/03/11 04:47] – Revise examples and remove mention of Javascript. crell | rfc:comprehensions [2019/04/05 01:10] (current) – Revise explanation of generator choice. crell | ||
---|---|---|---|
Line 31: | Line 31: | ||
</ | </ | ||
- | In both cases, '' | + | In both cases, '' |
===== Proposal ===== | ===== Proposal ===== | ||
Line 171: | Line 171: | ||
- In most cases it doesn' | - In most cases it doesn' | ||
- Cases where it does matter are where the list is especially large, or especially expensive to generate and only selected values will be used. In those cases a generator is superior as it minimizes the memory and CPU usage (respectively) needed to represent values. | - Cases where it does matter are where the list is especially large, or especially expensive to generate and only selected values will be used. In those cases a generator is superior as it minimizes the memory and CPU usage (respectively) needed to represent values. | ||
- | - If an actual array is desired, converting a generator to an array is a trivial call to '' | + | - If an actual array is desired, converting a generator to an array is a trivial call to '' |
+ | - That is, a greedy-list value can be composed out of a lazy-list value and a expansion operation. | ||
- A compact syntax to produce a generator allows for some nifty functional programming techniques that until now have been verbose to implement for non-array iterators. | - A compact syntax to produce a generator allows for some nifty functional programming techniques that until now have been verbose to implement for non-array iterators. | ||
Line 212: | Line 213: | ||
$result = array_map(function ($x) { | $result = array_map(function ($x) { | ||
- | $x * 2; | + | |
}, $list); | }, $list); | ||
Line 251: | Line 252: | ||
// but I include it for completeness. | // but I include it for completeness. | ||
$result = array_map(function($x) { | $result = array_map(function($x) { | ||
- | $x * 2 | + | |
}, array_filter(function() { | }, array_filter(function() { | ||
- | return $x % 2 | + | return $x % 2; |
}, $list)); | }, $list)); | ||
$result = (function() use ($list) { | $result = (function() use ($list) { | ||
foreach ($list as $x) { | foreach ($list as $x) { | ||
- | | + | |
yield $x * 2; | yield $x * 2; | ||
} | } | ||
Line 350: | Line 351: | ||
<code php> | <code php> | ||
$result = array_map(function($x) { | $result = array_map(function($x) { | ||
- | $x * 2 | + | |
}, array_filter(function() { | }, array_filter(function() { | ||
- | return $x % 2 | + | return $x % 2; |
}, $list)); | }, $list)); | ||
Line 371: | Line 372: | ||
<code php> | <code php> | ||
- | $result = (fn() => foreach($list as $x) if ($x % 2) yield $x * 2;)(); | + | $result = (fn() => foreach($list as $x) if ($x % 2) yield $x * 2)(); |
</ | </ | ||
rfc/comprehensions.1552279644.txt.gz · Last modified: 2019/03/11 04:47 by crell