Skip to content

Commit 7a3378a

Browse files
committed
Initial merge of Julien's suggestions.
2 parents ae7648f + 8760c07 commit 7a3378a

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

_overviews/overview/a-taste-of-scala.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,21 @@ val mi = 'C'
239239
val lastName = "Doe"
240240
```
241241

242-
String interpolation lets you combine those variables in a string like this:
242+
You can combine those variables in a string like this:
243243

244244
```scala
245245
println(s"Name: $firstName $mi $lastName") // "Name: John C Doe"
246246
```
247247

248248
Just precede the string with the letter `s`, and then put a `$` symbol before your variable names inside the string.
249249

250-
For expressions more complex than a single identifier, enclose the expression in curly braces:
250+
To enclose expressions inside a string, enclose them in curly braces:
251251

252-
```scala
252+
~~~ scala
253253
println(s"2 + 2 = ${2 + 2}") // prints "2 + 2 = 4"
254-
```
254+
val x = -1
255+
println(s"x.abs = ${x.abs}") // prints "x.abs = 1"
256+
~~~
255257

256258
#### Other interpolators
257259

@@ -347,7 +349,7 @@ else
347349
Note that this really is an *expression* — not a *statement* — meaning that it returns a value, so you can assign the result to a variable:
348350

349351
```scala
350-
val x = if (a < b) a else b
352+
val x = if a < b then a else b
351353
```
352354

353355
As you’ll see throughout this Overview and in our Reference documentation, *all* Scala control structures can be used as expressions.
@@ -369,7 +371,7 @@ The `for` keyword can be used to create a `for` loop. This example shows how to
369371
```scala
370372
val ints = List(1,2,3,4,5)
371373

372-
for (i <- ints) println(i)
374+
for i <- ints do println(i)
373375
```
374376

375377
<!--
@@ -410,15 +412,14 @@ We encourage you to make changes to that code to be sure you understand how it w
410412

411413
#### Using `for` as an expression
412414

413-
The `for` keyword has even more power: When you add the `yield` keyword to `for` loops, you create powerful `for` *expressions* which are used to calculate and yield results.
415+
The `for` keyword has even more power: When you use the `yield` keyword instead of `do`, you create `for` *expressions* which are used to calculate and yield results.
414416

415417
A few examples demonstrate this. Using the same `ints` list as the previous example, this code creates a new list, where the value of each element in the new list is twice the value of the elements in the original list:
416418

417419
````
418420
scala> val doubles = for (i <- ints) yield i * 2
419421
val doubles: List[Int] = List(2, 4, 6, 8, 10)
420422
````
421-
422423
Note that Scala’s syntax is flexible, and that `for` expression can be written in several different ways to make your code more readable:
423424

424425
```scala
@@ -591,7 +592,7 @@ trait Runner:
591592
Given those traits, here’s a `Dog` class that extends all of those traits while providing a behavior for the abstract `speak` method:
592593

593594
```scala
594-
class Dog(name: String) extends Speaker with TailWagger with Runner:
595+
class Dog(name: String) extends Speaker, TailWagger, Runner:
595596
def speak(): String = "Woof!"
596597
```
597598

@@ -600,7 +601,7 @@ Notice how the class extends the traits with the `extends` and `with` keywords.
600601
Similarly, here’s a `Cat` class that implements those same traits while also overriding two of the concrete methods it inherits:
601602

602603
```scala
603-
class Cat(name: String) extends Speaker with TailWagger with Runner:
604+
class Cat(name: String) extends Speaker, TailWagger, Runner:
604605
def speak(): String = "Meow"
605606
override def startRunning(): Unit = println("Yeah ... I don’t run")
606607
override def stopRunning(): Unit = println("No need to stop")
@@ -652,7 +653,7 @@ p.printFullName() // "Julia Manes"
652653
```
653654

654655

655-
### FP data modeling
656+
### Data Modeling (Functional Programming Style)
656657

657658
When writing code in an FP style, you’ll use these constructs:
658659

@@ -754,7 +755,7 @@ val p = Person("Reginald Kenneth Dwight", "Singer")
754755
// a good default toString method
755756
p // Person = Person(Reginald Kenneth Dwight,Singer)
756757

757-
// can access its fields, but they are immutable
758+
// can access its fields, which are immutable
758759
p.name // "Reginald Kenneth Dwight"
759760
p.name = "Joe" // error: can’t reassign a val field
760761

@@ -1313,11 +1314,3 @@ Scala has even more features that weren’t covered in this whirlwind tour. See
13131314

13141315

13151316

1316-
1317-
1318-
1319-
1320-
1321-
1322-
1323-

0 commit comments

Comments
 (0)