From b25baf137a8e52aa115af4a679d8f924a083b4c7 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:19:34 -0800 Subject: [PATCH 01/12] Create syntax-list-comprehensions --- rfcs/syntax-list-comprehensions | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 rfcs/syntax-list-comprehensions diff --git a/rfcs/syntax-list-comprehensions b/rfcs/syntax-list-comprehensions new file mode 100644 index 00000000..cdd3388e --- /dev/null +++ b/rfcs/syntax-list-comprehensions @@ -0,0 +1,14 @@ +## Summary + +Introduce a form of list comprehension using `for var in iterator do` syntax. + +## Motivation + +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 + +## 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`. From 64cd591b96477a9669e86f6de78387a715fea9df Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:19:53 -0800 Subject: [PATCH 02/12] Rename syntax-list-comprehensions to syntax-list-comprehensions.md --- .../{syntax-list-comprehensions => syntax-list-comprehensions.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rfcs/{syntax-list-comprehensions => syntax-list-comprehensions.md} (100%) diff --git a/rfcs/syntax-list-comprehensions b/rfcs/syntax-list-comprehensions.md similarity index 100% rename from rfcs/syntax-list-comprehensions rename to rfcs/syntax-list-comprehensions.md 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 03/12] 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`` From 17c0462665a9e5cebba7ef4d11ce2010779b628f Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:37:47 -0800 Subject: [PATCH 04/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 3abd5fe9..f764c43e 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -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 `` for in 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. From e52d0360000c5e70987d337ab1b5aec016a51875 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:38:12 -0800 Subject: [PATCH 05/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index f764c43e..71830b2e 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -62,4 +62,4 @@ let vector = c![x, for x in 1..10, if x % 2 == 0]; ``` ### Function syntax -List comprehensions could be implemented as functions like ``table.map`` or ``table.filter`` +List comprehensions could be implemented as functions with ``table.map`` or ``table.filter`` From 14e40671fc5c7c6f70b520c4e2372b48a6589791 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:39:20 -0800 Subject: [PATCH 06/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 71830b2e..8708e43b 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -63,3 +63,15 @@ 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`` + +Examples: + +#### Perl +```perl +my @doubles = map {$_*2} 1..9; +``` + +#### Rust +```rust +let ns: Vec<_> = (0..100).filter(|x| x * x > 3).map(|x| 2 * x).collect(); +``` From 5f0b8ae1be0fcee6b9a1b0694e23fc3b3e194c4e Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:40:11 -0800 Subject: [PATCH 07/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 8708e43b..2ce510d2 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -7,7 +7,7 @@ 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 +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 ## Design From a8593fca7695f3f16cf8510afe4913beb2fd777d Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:40:27 -0800 Subject: [PATCH 08/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 2ce510d2..bfe0b572 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -7,7 +7,7 @@ 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 +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 From 9361b856da7fd709d90af73899bfbd296e3270c4 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:41:10 -0800 Subject: [PATCH 09/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index bfe0b572..386b3c58 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -28,7 +28,7 @@ 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} +local onlyEven = {for n in t do if n%2 == 0 then n} ``` ## Drawbacks From 37da546e8d9ec9fd9bf8a1b4407ed22f2a6f7c24 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:41:44 -0800 Subject: [PATCH 10/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 386b3c58..4f5fe9c8 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -61,10 +61,10 @@ let vector = c![x, for x in 1..10, if x % 2 == 0]; S <- 2 * x[x ^ 2 > 3] ``` -### Function syntax +## Function syntax List comprehensions could be implemented as functions with ``table.map`` or ``table.filter`` -Examples: +### Examples: #### Perl ```perl From 6cefa03519a5b0270f2228da97e6f5f6eed1ab24 Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:45:12 -0800 Subject: [PATCH 11/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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: From cf0bc1ed933af0d1623b9f06aafe04e88cc1daff Mon Sep 17 00:00:00 2001 From: James Napora <85808999+TheGreatSageEqualToHeaven@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:53:03 -0800 Subject: [PATCH 12/12] Update syntax-list-comprehensions.md --- rfcs/syntax-list-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfcs/syntax-list-comprehensions.md b/rfcs/syntax-list-comprehensions.md index 3a09a4b4..7a8ccb7d 100644 --- a/rfcs/syntax-list-comprehensions.md +++ b/rfcs/syntax-list-comprehensions.md @@ -14,7 +14,7 @@ When traversing a table to exclude all the odd numbers you'd be creating a large 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``. -A ``if then`` expression might be required. +A ``if `` expression might be required. Example: ```lua