Skip to content

Commit c8a1af2

Browse files
committed
Merge pull request scala#106 from BrownFurSeal/patch-1
Add low-level iteration with for expression
2 parents f6702dd + 42f7b67 commit c8a1af2

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

‎cheatsheets/index.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ about: Thanks to <a href="http://brenocon.com/">Brendan O'Connor</a>, this cheat
5757
|`for ((x,y) <- xs zip ys) yield x*y`_same as_ <br>`(xs zip ys) map{case (x,y) => x*y }`| for comprehension: destructuring bind |
5858
|`for (x <- xs; y <- ys) yield x*y`_same as_ <br>`xs flatMap{x => ys map{y => x*y}}`| for comprehension: cross product |
5959
|`for (x <- xs; y <- ys){`<br> `println("%d/%d = %.1f".format(x,y, x*y))`<br>`}`| for comprehension: imperative-ish<br>[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax)|
60+
|`for (i <- 1 to 5){`<br> `println(i)`<br>`}`| for comprehension: iterate including the upper bound |
61+
|`for (i <- 1 until 5){`<br> `println(i)`<br>`}`| for comprehension: iterate omitting the upper bound |
6062
| <h2id="pattern_matching">pattern matching</h2> ||
6163
| <spanclass="label success">Good</span> `(xs zip ys) map{case (x,y) => x*y }`<br> <spanclass="label important">Bad</span> `(xs zip ys) map( (x,y) => x*y )`| use case in function args for pattern matching. |
6264
| <spanclass="label important">Bad</span><br>`val v42 = 42`<br>`Some(3) match{`<br>` case Some(v42) => println("42")`<br>` case _ => println("Not 42")`<br>`}`| "v42" is interpreted as a name matching any Int value, and "42" is printed. |

0 commit comments

Comments
(0)