rfc:comprehensions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:comprehensions [2019/03/10 21:36] – Fix wiki syntax issues crell | rfc:comprehensions [2019/03/11 11:45] – nikic | ||
---|---|---|---|
Line 31: | Line 31: | ||
</ | </ | ||
- | In both cases, '' | + | In both cases, '' |
===== Proposal ===== | ===== Proposal ===== | ||
Line 245: | Line 245: | ||
<code php> | <code php> | ||
- | $list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | + | $list = range(1, 10); |
// In practice you'd almost always just use a | // In practice you'd almost always just use a | ||
// foreach() rather than this monstrosity, | // foreach() rather than this monstrosity, | ||
// but I include it for completeness. | // but I include it for completeness. | ||
- | $result = array_filter(array_map(function ($x) { | + | $result = array_map(function($x) { |
- | $x * 2; | + | $x * 2 |
- | }, $list), | + | }, array_filter(function() { |
- | return $x % 2; | + | return $x % 2 |
- | }); | + | }, $list)); |
$result = (function() use ($list) { | $result = (function() use ($list) { | ||
Line 338: | Line 338: | ||
Numerous languages include a comprehension syntax of some form (https:// | Numerous languages include a comprehension syntax of some form (https:// | ||
- | Two of the languages PHP developers are most likely to also use, JavaScript and Python, feature a very similar | + | The syntax proposed here was initially based on Python' |
+ | |||
+ | If a more terse syntax | ||
Note that in Python 2.x list comprehensions produce a complete list. In Python 3.x they produce a generator that will, in turn, produce a complete list. That change has been a source of incompatibility between Python 2.x and 3.x code. This RFC proposes using generators exclusively for comprehensions. | Note that in Python 2.x list comprehensions produce a complete list. In Python 3.x they produce a generator that will, in turn, produce a complete list. That change has been a source of incompatibility between Python 2.x and 3.x code. This RFC proposes using generators exclusively for comprehensions. | ||
- | |||
- | If a more terse syntax that is still lexer-friendly can be proposed that may be adopted instead of the syntax proposed here. | ||
===== Comparison to other proposals ===== | ===== Comparison to other proposals ===== | ||
- | The "sort lambda" | + | The "short lambda" |
<code php> | <code php> | ||
- | $result = array_filter(array_map(function ($x) { | + | $result = array_map(function($x) { |
- | $x * 2; | + | $x * 2 |
- | }, $list), | + | }, array_filter(function() { |
- | return $x % 2; | + | return $x % 2 |
- | }); | + | }, $list)); |
$result = [for $list as $x if $x % 2 yield $x * 2]; | $result = [for $list as $x if $x % 2 yield $x * 2]; | ||
Line 359: | Line 359: | ||
The arrow function equivalent would be: | The arrow function equivalent would be: | ||
+ | Which, while unquestionably an improvement over the array_map/ | ||
<code php> | <code php> | ||
Line 367: | Line 368: | ||
</ | </ | ||
- | Which, while unquestionably | + | Or potentially: |
+ | |||
+ | <code php> | ||
+ | $result = (fn() => foreach($list as $x) if ($x % 2) yield $x * 2;)(); | ||
+ | </ | ||
+ | |||
+ | Either is definitely | ||
That said, there are ample other cases where arrow functions would be useful so the adoption of this RFC should in no way be seen to detract from their benefit. | That said, there are ample other cases where arrow functions would be useful so the adoption of this RFC should in no way be seen to detract from their benefit. |
rfc/comprehensions.txt · Last modified: 2019/04/05 01:10 by crell