From fb1c5c2f1e978ec731f99c8d434e36e0ce795cf0 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:36:52 -0800 Subject: [PATCH] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index cdd3388e..3abd5fe9 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -1,6 +1,6 @@ ## Summary -Introduce a form of list comprehension using `for var in iterator do` syntax. +Introduce a form of list comprehension using `n for-in-do` syntax. ## Motivation @@ -12,3 +12,38 @@ When traversing a table to exclude all the odd numbers you'd be creating a large ## 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`` + +## Drawbacks + +List comprehensions may be misused. + +The list comprehensions have similar drawbacks to the if expression drawbacks +https://github.com/Roblox/luau/blob/master/rfcs/syntax-if-expression.md + +## Alternatives + +### C# +```csharp +var list2 = from number in Enumerable.Range(0, 10) select 2*number; +``` + +### Rust (using cute) +```rust +let vector = c![x, for x in 1..10, if x % 2 == 0]; +``` + +### Haskell +```haskell +[x * 2 | x <- [0 .. 99], x * x > 3] +``` + +### R +```r + x <- 0:100 + S <- 2 * x[x ^ 2 > 3] + ``` + +### Function syntax +List comprehensions could be implemented as functions like ``table.map`` or ``table.filter``