diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 4f5fe9c8..3a09a4b4 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -7,13 +7,14 @@ Introduce a form of list comprehension using `n for-in-do` syntax. List comprehensions would bring several benefits and prevent code smell. In Lua you are encouraged to not modify tables during traversal. -When traversing a table to exclude all the odd numbers you'd be creating a large statement to get rid of them, with list comprehensions this could be shortened and cleaner +When traversing a table to exclude all the odd numbers you'd be creating a large statement to get rid of them, with list comprehensions this could be shortened and cleaner. ## Design To solve these problems, I propose a `n for-in-do` expression form that is syntactically similar to a for statement, but lacks terminating `end`. -The `n for-in-do` expression must match `` for in do`` +The `n for-in-do` expression must match `` for in do``. +A ``if then`` expression might be required. Example: ```lua @@ -28,14 +29,14 @@ end -- list comprehensions local t = {1,2,3,4,5,6,7,8,9} -local onlyEven = {for n in t do if n%2 == 0 then n} +local onlyEven = {n for n in t do if n%2 == 0} ``` ## Drawbacks List comprehensions may be misused. -The list comprehensions have similar drawbacks to the if expression drawbacks +The list comprehensions have similar drawbacks to the if expression drawbacks. https://github.com/Roblox/luau/blob/master/rfcs/syntax-if-expression.md ## Alternatives @@ -62,7 +63,7 @@ let vector = c![x, for x in 1..10, if x % 2 == 0]; ``` ## Function syntax -List comprehensions could be implemented as functions with ``table.map`` or ``table.filter`` +List comprehensions could be implemented as functions with ``table.map`` or ``table.filter`` . ### Examples: