From bcb1b623b1db1057085982507399c62eaa053e7b Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Fri, 23 Jun 2017 13:35:19 -0700 Subject: [scala/en] Make return value example actually demonstrate issue Previously the `return z` didn't actually have any effect on the output, since the outer function just return the anon function's result directly. Updated to make the outer function do something to demonstrate the difference. Also renamed functions to make what they're doing easier to follow, and added a couple examples of behavior w/ explanations --- scala.html.markdown | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 5eb94986..e541f4b3 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -253,16 +253,20 @@ weirdSum(2, 4) // => 16 // def that surrounds it. // WARNING: Using return in Scala is error-prone and should be avoided. // It has no effect on anonymous functions. For example: -def foo(x: Int): Int = { - val anonFunc: Int => Int = { z => +def addTenButMaybeTwelve(x: Int): Int = { + val anonMaybeAddTwo: Int => Int = { z => if (z > 5) - return z // This line makes z the return value of foo! + return z // This line makes z the return value of addTenButMaybeTwelve! else z + 2 // This line is the return value of anonFunc } - anonFunc(x) // This line is the return value of foo + anonMaybeAddTwo(x) + 10 // This line is the return value of foo } +addTenButMaybeTwelve(2) // Returns 14 as expected: 2 <= 5, adds 12 +addTenButMaybeTwelve(7) // Returns 7: 7 > 5, return value set to z, so + // last line doesn't get called and 10 is not added + ///////////////////////////////////////////////// // 3. Flow Control -- cgit v1.2.3 From 23cee36b4c056dcd3c01676132cb9a6588fb75ad Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Thu, 29 Jun 2017 10:49:44 -0700 Subject: Update more function mentions in comments --- scala.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index e541f4b3..192af953 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -258,9 +258,9 @@ def addTenButMaybeTwelve(x: Int): Int = { if (z > 5) return z // This line makes z the return value of addTenButMaybeTwelve! else - z + 2 // This line is the return value of anonFunc + z + 2 // This line is the return value of anonMaybeAddTwo } - anonMaybeAddTwo(x) + 10 // This line is the return value of foo + anonMaybeAddTwo(x) + 10 // This line is the return value of addTenButMaybeTwelve } addTenButMaybeTwelve(2) // Returns 14 as expected: 2 <= 5, adds 12 -- cgit v1.2.3 From 52a4a4ac95f3df89b574458ec8e951458b2f8f85 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Wed, 28 Feb 2018 02:44:18 -0800 Subject: [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 --- scala.html.markdown | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 78053b40..78893b30 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -276,6 +276,7 @@ r foreach println // 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 +// Why doesn't `println` need any parameters here? Stay tuned for Functional Programming below! (5 to 1 by -1) foreach (println) // A while loop @@ -299,7 +300,7 @@ do { // Recursion is the idiomatic way of repeating an action in Scala (as in most // other functional languages). // 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 = { print(a) if (a < b) @@ -412,8 +413,8 @@ class Dog(br: String) { private def sleep(hours: Int) = println(s"I'm sleeping for $hours hours") - // Abstract methods are simply methods with no body. If we uncomment the next - // line, class Dog would need to be declared abstract + // Abstract methods are simply methods with no body. If we uncomment the + // def line below, class Dog would need to be declared abstract like so: // abstract class Dog(...) { ... } // def chaseAfter(what: String): String } @@ -455,7 +456,7 @@ george.phoneNumber // => "1234" Person("George", "1234") == Person("Kate", "1236") // => false // Easy way to copy -// otherGeorge == Person("george", "9876") +// otherGeorge == Person("George", "9876") val otherGeorge = george.copy(phoneNumber = "9876") // 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 } -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 // 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 +// Using `s` from above: +// val s = Set(1, 3, 7) s.map(sq) @@ -608,8 +613,8 @@ List( ).filter(_.age > 25) // List(Person("Bob", 30)) -// Scala a foreach method defined on certain collections that takes a type -// returning Unit (a void method) +// Certain collections (such as List) in Scala have a `foreach` 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) aListOfNumbers foreach (x => println(x)) aListOfNumbers foreach println -- cgit v1.2.3 From 03b9fce5fa97b11931c3358964413591c0633f45 Mon Sep 17 00:00:00 2001 From: ven Date: Wed, 28 Feb 2018 11:44:53 +0100 Subject: follow-up for #2768 --- scala.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 78893b30..016e2b4f 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -276,7 +276,8 @@ r foreach println // 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 -// Why doesn't `println` need any parameters here? Stay tuned for Functional Programming below! +// Why doesn't `println` need any parameters here? +// Stay tuned for first-class functions in the Functional Programming section below! (5 to 1 by -1) foreach (println) // A while loop -- cgit v1.2.3 From a462b444e06f75c9f98983e925b8f79a1092d277 Mon Sep 17 00:00:00 2001 From: Brian Stearns Date: Thu, 31 May 2018 15:53:10 -0400 Subject: Correct English a/an usage --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 016e2b4f..28424684 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -716,7 +716,7 @@ import scala.collection.immutable.{Map => _, Set => _, _} // Java classes can also be imported. Scala syntax can be used import java.swing.{JFrame, JWindow} -// Your programs entry point is defined in an scala file using an object, with a +// Your programs entry point is defined in a scala file using an object, with a // single method, main: object Application { def main(args: Array[String]): Unit = { -- cgit v1.2.3 From 6d087ae0f289e7013807639cbd609f991f968166 Mon Sep 17 00:00:00 2001 From: ven Date: Sat, 8 Sep 2018 23:29:35 +0200 Subject: Revert "[scala/en] Make return value example actually demonstrate issue" (#3213) --- scala.html.markdown | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 7429ac9a..28424684 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -253,20 +253,16 @@ weirdSum(2, 4) // => 16 // def that surrounds it. // WARNING: Using return in Scala is error-prone and should be avoided. // It has no effect on anonymous functions. For example: -def addTenButMaybeTwelve(x: Int): Int = { - val anonMaybeAddTwo: Int => Int = { z => +def foo(x: Int): Int = { + val anonFunc: Int => Int = { z => if (z > 5) - return z // This line makes z the return value of addTenButMaybeTwelve! + return z // This line makes z the return value of foo! else - z + 2 // This line is the return value of anonMaybeAddTwo + z + 2 // This line is the return value of anonFunc } - anonMaybeAddTwo(x) + 10 // This line is the return value of addTenButMaybeTwelve + anonFunc(x) // This line is the return value of foo } -addTenButMaybeTwelve(2) // Returns 14 as expected: 2 <= 5, adds 12 -addTenButMaybeTwelve(7) // Returns 7: 7 > 5, return value set to z, so - // last line doesn't get called and 10 is not added - ///////////////////////////////////////////////// // 3. Flow Control -- cgit v1.2.3 From 5b6a6e2adbec0e97c9e79d84a402bfff8983ddcb Mon Sep 17 00:00:00 2001 From: Mahmoud Hossam Date: Tue, 2 Jul 2019 09:46:18 +0200 Subject: Update scala docs link for Map --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 28424684..c7a8842e 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -345,7 +345,7 @@ s(0) // Boolean = false s(1) // Boolean = true /* Look up the documentation of map here - - * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map + * https://www.scala-lang.org/api/current/scala/collection/immutable/Map.html * and make sure you can read it */ -- cgit v1.2.3