[scala/en] A few editing improvements as I read through (#2768)

* A few editing improvements as I read through

Take, leave, or modify as desired!

Specifically:
* Acknowledge weirdness of no parameters in `foreach println`
 * Mention what `Unit` is
 * Clarify abstract comments
 * Fix capitalization of George in example
 * Explicitly introduce regex
 * Re-iterate `s` in comments, it's gotten very separated

* Reword explanation of foreach
This commit is contained in:
Joel Bradshaw 2018-02-28 02:44:18 -08:00 committed by ven
parent a526d2f4b4
commit 52a4a4ac95

View File

@ -276,6 +276,7 @@ r foreach println
// NB: Scala is quite lenient when it comes to dots and brackets - study the // NB: Scala is quite lenient when it comes to dots and brackets - study the
// rules separately. This helps write DSLs and APIs that read like English // rules separately. This helps write DSLs and APIs that read like English
// Why doesn't `println` need any parameters here? Stay tuned for Functional Programming below!
(5 to 1 by -1) foreach (println) (5 to 1 by -1) foreach (println)
// A while loop // A while loop
@ -299,7 +300,7 @@ do {
// Recursion is the idiomatic way of repeating an action in Scala (as in most // Recursion is the idiomatic way of repeating an action in Scala (as in most
// other functional languages). // other functional languages).
// Recursive functions need an explicit return type, the compiler can't infer it. // Recursive functions need an explicit return type, the compiler can't infer it.
// Here it's Unit. // Here it's Unit, which is analagous to a `void` return type in Java
def showNumbersInRange(a: Int, b: Int): Unit = { def showNumbersInRange(a: Int, b: Int): Unit = {
print(a) print(a)
if (a < b) if (a < b)
@ -412,8 +413,8 @@ class Dog(br: String) {
private def sleep(hours: Int) = private def sleep(hours: Int) =
println(s"I'm sleeping for $hours hours") println(s"I'm sleeping for $hours hours")
// Abstract methods are simply methods with no body. If we uncomment the next // Abstract methods are simply methods with no body. If we uncomment the
// line, class Dog would need to be declared abstract // def line below, class Dog would need to be declared abstract like so:
// abstract class Dog(...) { ... } // abstract class Dog(...) { ... }
// def chaseAfter(what: String): String // def chaseAfter(what: String): String
} }
@ -455,7 +456,7 @@ george.phoneNumber // => "1234"
Person("George", "1234") == Person("Kate", "1236") // => false Person("George", "1234") == Person("Kate", "1236") // => false
// Easy way to copy // Easy way to copy
// otherGeorge == Person("george", "9876") // otherGeorge == Person("George", "9876")
val otherGeorge = george.copy(phoneNumber = "9876") val otherGeorge = george.copy(phoneNumber = "9876")
// And many others. Case classes also get pattern matching for free, see below. // And many others. Case classes also get pattern matching for free, see below.
@ -523,7 +524,9 @@ def matchPerson(person: Person): String = person match {
case Person(name, number) => "We matched someone : " + name + ", phone : " + number case Person(name, number) => "We matched someone : " + name + ", phone : " + number
} }
val email = "(.*)@(.*)".r // Define a regex for the next example. // Regular expressions are also built in.
// Create a regex with the `r` method on a string:
val email = "(.*)@(.*)".r
// Pattern matching might look familiar to the switch statements in the C family // Pattern matching might look familiar to the switch statements in the C family
// of languages, but this is much more powerful. In Scala, you can match much // of languages, but this is much more powerful. In Scala, you can match much
@ -589,6 +592,8 @@ List("Dom", "Bob", "Natalia") foreach println
// Combinators // Combinators
// Using `s` from above:
// val s = Set(1, 3, 7)
s.map(sq) s.map(sq)
@ -608,8 +613,8 @@ List(
).filter(_.age > 25) // List(Person("Bob", 30)) ).filter(_.age > 25) // List(Person("Bob", 30))
// Scala a foreach method defined on certain collections that takes a type // Certain collections (such as List) in Scala have a `foreach` method,
// returning Unit (a void method) // which takes as an argument a type returning Unit - that is, a void method
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100) val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
aListOfNumbers foreach (x => println(x)) aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println aListOfNumbers foreach println