Update syntax-list-comprehensions.md

This commit is contained in:
James Napora 2022-02-13 16:37:47 -08:00 committed by GitHub
parent fb1c5c2f1e
commit 17c0462665
Signed by: DevComp
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,22 @@ To solve these problems, I propose a `n for-in-do` expression form that is synta
The `n for-in-do` expression must match ``<identifier> for <identifier> in <expr> do``
Example:
```lua
-- normal
local t = {1,2,3,4,5,6,7,8,9}
local onlyEven = {}
for i,n in pairs(t) do
if n%2 == 0 then
table.insert(onlyEven, n)
end
end
-- list comprehensions
local t = {1,2,3,4,5,6,7,8,9}
local onlyEven = {local n for n in t do if n%2 == 0 then n}
```
## Drawbacks
List comprehensions may be misused.