forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add improvements to for comprehensions
- Allow `for`-comprehensions to start with aliases desugaring them into valdefs in a new block - Desugar aliases into simple valdefs, instead of patterns when they are not followed by a guard - Add an experimental language flag that enables the new desugaring method
- Loading branch information
1 parent
bd0aa52
commit 9c3e454
Showing
8 changed files
with
263 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
List((1,3), (1,4), (2,3), (2,4)) | ||
List((1,2,3), (1,2,4)) | ||
List((1,3), (1,4), (2,3), (2,4)) | ||
List((2,3), (2,4)) | ||
List((2,3), (2,4)) | ||
List((1,2), (2,4)) | ||
List(1, 2, 3) | ||
List((2,3,6)) | ||
List(6) | ||
List(3, 6) | ||
List(6) | ||
List(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import scala.language.experimental.betterFors | ||
|
||
def for1 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
c <- List(3, 4) | ||
} yield (b, c) | ||
|
||
def for2 = | ||
for | ||
a = 1 | ||
b = 2 | ||
c <- List(3, 4) | ||
yield (a, b, c) | ||
|
||
def for3 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
c = 3 | ||
d <- List(c, 4) | ||
} yield (b, d) | ||
|
||
def for4 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
if b > 1 | ||
c <- List(3, 4) | ||
} yield (b, c) | ||
|
||
def for5 = | ||
for { | ||
a = 1 | ||
b <- List(a, 2) | ||
c = 3 | ||
if b > 1 | ||
d <- List(c, 4) | ||
} yield (b, d) | ||
|
||
def for6 = | ||
for { | ||
a = 1 | ||
b = 2 | ||
c <- for { | ||
x <- List(a, b) | ||
y = x * 2 | ||
} yield (x, y) | ||
} yield c | ||
|
||
def for7 = | ||
for { | ||
a <- List(1, 2, 3) | ||
} yield a | ||
|
||
def for8 = | ||
for { | ||
a <- List(1, 2) | ||
b = a + 1 | ||
if b > 2 | ||
c = b * 2 | ||
if c < 8 | ||
} yield (a, b, c) | ||
|
||
def for9 = | ||
for { | ||
a <- List(1, 2) | ||
b = a * 2 | ||
if b > 2 | ||
} yield a + b | ||
|
||
def for10 = | ||
for { | ||
a <- List(1, 2) | ||
b = a * 2 | ||
} yield a + b | ||
|
||
def for11 = | ||
for { | ||
a <- List(1, 2) | ||
b = a * 2 | ||
if b > 2 && b % 2 == 0 | ||
} yield a + b | ||
|
||
def for12 = | ||
for { | ||
a <- List(1, 2) | ||
if a > 1 | ||
} yield a | ||
|
||
object Test extends App { | ||
println(for1) | ||
println(for2) | ||
println(for3) | ||
println(for4) | ||
println(for5) | ||
println(for6) | ||
println(for7) | ||
println(for8) | ||
println(for9) | ||
println(for10) | ||
println(for11) | ||
println(for12) | ||
} |
Oops, something went wrong.