From 6027c90c90a55702d003f17a72fd82ed7f870be0 Mon Sep 17 00:00:00 2001 From: Artur Mkrtchyan Date: Sat, 20 Dec 2014 23:03:25 +0100 Subject: Scala compiler fails to infer the return type when there's an explicit return statement --- 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 5a478f2a..35645922 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -199,7 +199,7 @@ weirdSum(2, 4) // => 16 // The return keyword exists in Scala, but it only returns from the inner-most // def that surrounds it. It has no effect on anonymous functions. For example: -def foo(x: Int) = { +def foo(x: Int): Int = { val anonFunc: Int => Int = { z => if (z > 5) return z // This line makes z the return value of foo! -- cgit v1.2.3 From 6dca0e7ae41140765cb3b0c1513cfa897294efe5 Mon Sep 17 00:00:00 2001 From: Artur Mkrtchyan Date: Sun, 21 Dec 2014 13:08:21 +0100 Subject: Fixed Person class' constructor signature --- 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 5a478f2a..544abd5b 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -476,7 +476,7 @@ sSquared.reduce (_+_) // The filter function takes a predicate (a function from A -> Boolean) and // selects all elements which satisfy the predicate List(1, 2, 3) filter (_ > 2) // List(3) -case class Person(name:String, phoneNumber:String) +case class Person(name:String, age:Int) List( Person(name = "Dom", age = 23), Person(name = "Bob", age = 30) -- cgit v1.2.3 From d7672a2bc53dd8b81c4da8c620178cc2458c9238 Mon Sep 17 00:00:00 2001 From: Artur Mkrtchyan Date: Sat, 3 Jan 2015 19:48:53 +0100 Subject: Added warning on return usage --- scala.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 35645922..cfc5a1e9 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -198,7 +198,9 @@ weirdSum(2, 4) // => 16 // The return keyword exists in Scala, but it only returns from the inner-most -// def that surrounds it. It has no effect on anonymous functions. For example: +// 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 => if (z > 5) -- cgit v1.2.3 From 8f29b15cda9cbc7ca8fc1f31ed0755245c9fc9c7 Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Sun, 11 Jan 2015 15:30:12 -0700 Subject: Rewrite the pattern matching section --- scala.html.markdown | 64 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 25 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 336251ba..3fa4d4b8 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -198,8 +198,8 @@ weirdSum(2, 4) // => 16 // The return keyword exists in Scala, but it only returns from the inner-most -// def that surrounds it. -// WARNING: Using return in Scala is error-prone and should be avoided. +// 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 => @@ -407,41 +407,55 @@ val otherGeorge = george.copy(phoneNumber = "9876") // 6. Pattern Matching ///////////////////////////////////////////////// -val me = Person("George", "1234") +// Pattern matching is a powerful and commonly used feature in Scala. Here's how +// you pattern match a case class. NB: Unlike other languages, Scala cases do +// not have breaks. -me match { case Person(name, number) => { - "We matched someone : " + name + ", phone : " + number }} - -me match { case Person(name, number) => "Match : " + name; case _ => "Hm..." } +def matchPerson(person: Person): String = person match { + // Then you specify the patterns: + case Person("George", number) => "We found George! His number is " + number + case Person("Kate", number) => "We found Kate! Her number is " + number + case Person(name, number) => "We matched someone : " + name + ", phone : " + number +} -me match { case Person("George", number) => "Match"; case _ => "Hm..." } +val email = "(.*)@(.*)".r // Define a regex for the next example. -me match { case Person("Kate", number) => "Match"; case _ => "Hm..." } +// 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 +// more: +def matchEverything(obj: Any): String = obj match { + // You can match values: + case "Hello world" => "Got the string Hello world" -me match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" } + // You can match by type: + case x: Double => "Got a Double: " + x -val kate = Person("Kate", "1234") + // You can specify conditions: + case x: Int if x > 10000 => "Got a pretty big number!" -kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" } + // You can match case classes as before: + case Person(name, number) => s"Got contact info for $name!" + // You can match regular expressions: + case email(name, domain) => s"Got email address $name@$domain" + // You can match tuples: + case (a: Int, b: Double, c: String) => s"Got a tuple: $a, $b, $c" -// Regular expressions -val email = "(.*)@(.*)".r // Invoking r on String makes it a Regex -val serialKey = """(\d{5})-(\d{5})-(\d{5})-(\d{5})""".r // Using verbatim (multiline) syntax + // You can match data structures: + case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c" -val matcher = (value: String) => { - println(value match { - case email(name, domain) => s"It was an email: $name" - case serialKey(p1, p2, p3, p4) => s"Serial key: $p1, $p2, $p3, $p4" - case _ => s"No match on '$value'" // default if no match found - }) + // You can nest patterns: + case List(List((1, 2,"YAY"))) => "Got a list of list of tuple" } -matcher("mrbean@pyahoo.com") // => "It was an email: mrbean" -matcher("nope..") // => "No match on 'nope..'" -matcher("52917") // => "No match on '52917'" -matcher("52752-16432-22178-47917") // => "Serial key: 52752, 16432, 22178, 47917" +// In fact, you can pattern match any object with an "unapply" method. This +// feature is so powerful that Scala lets you define whole functions as +// patterns: +val patternFunc: Person => String = { + case Person("George", number") => s"George's number: $number" + case Person(name, number) => s"Random person's number: $number" +} ///////////////////////////////////////////////// -- cgit v1.2.3 From c053f1559bb357d9e8ced2452096bf3a95cc7ddb Mon Sep 17 00:00:00 2001 From: Geoff Liu Date: Sun, 11 Jan 2015 18:11:06 -0700 Subject: Better wording about breaks in cases --- 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 3fa4d4b8..61c735e3 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -409,7 +409,7 @@ val otherGeorge = george.copy(phoneNumber = "9876") // Pattern matching is a powerful and commonly used feature in Scala. Here's how // you pattern match a case class. NB: Unlike other languages, Scala cases do -// not have breaks. +// not need breaks, fall-through does not happen. def matchPerson(person: Person): String = person match { // Then you specify the patterns: -- cgit v1.2.3 From 73025e65c3f0729ae68c8967b6382de2fedbb13f Mon Sep 17 00:00:00 2001 From: Ikrom Date: Thu, 29 Jan 2015 09:03:36 +0500 Subject: Update scala.html.markdown Removed typo (unnecessary double quote in patternFunc) --- 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 61c735e3..ed1ddabb 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -453,7 +453,7 @@ def matchEverything(obj: Any): String = obj match { // feature is so powerful that Scala lets you define whole functions as // patterns: val patternFunc: Person => String = { - case Person("George", number") => s"George's number: $number" + case Person("George", number) => s"George's number: $number" case Person(name, number) => s"Random person's number: $number" } -- cgit v1.2.3 From 4adbf231c805afdf68fed3a5e2aba9c90fd4c0c1 Mon Sep 17 00:00:00 2001 From: yejinchang Date: Thu, 16 Apr 2015 17:53:40 +0800 Subject: make corrections --- 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 ed1ddabb..e6638121 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -186,7 +186,7 @@ val sq: Int => Int = x => x * x // Anonymous functions can be called as usual: sq(10) // => 100 -// If your anonymous function has one or two arguments, and each argument is +// If each argument in your anonymous function is // used only once, Scala gives you an even shorter way to define them. These // anonymous functions turn out to be extremely common, as will be obvious in // the data structure section. @@ -465,6 +465,7 @@ val patternFunc: Person => String = { // Scala allows methods and functions to return, or take as parameters, other // functions or methods. +val add10: Int => Int = _ + 10 // A function taking an Int and returning an Int List(1, 2, 3) map add10 // List(11, 12, 13) - add10 is applied to each element // Anonymous functions can be used instead of named functions: -- cgit v1.2.3 From 496b11f8ce33f38fc1e29c23ee8c4261a811b967 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:47:02 +0700 Subject: scala: consistent style, language correction --- scala.html.markdown | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index e6638121..12441af9 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -16,15 +16,16 @@ Scala - the scalable language Set yourself up: 1) Download Scala - http://www.scala-lang.org/downloads - 2) unzip/untar in your favourite location and put the bin subdir on the path - 3) Start a scala REPL by typing scala. You should see the prompt: + 2) Unzip/untar to your favourite location and put the bin subdir in your `PATH` environment variable + 3) Start a Scala REPL by running `scala`. You should see the prompt: scala> - This is the so called REPL (Read-Eval-Print Loop). You may type any valid - Scala expression into it, and the result will be printed. We will explain what - Scala files look like further into this tutorial, but for now, let's start - with some basics. + This is the so called REPL (Read-Eval-Print Loop). You may type any Scala + expression, and the result will be printed. We will explain what Scala files + look like further into this tutorial, but for now, let's start with some + basics. + */ -- cgit v1.2.3 From 9ddf69a14da14c0d29abb5a18b4d2dfd5a39c7b2 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:48:28 +0700 Subject: scala: language correction --- scala.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 12441af9..4121dc9b 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -5,6 +5,7 @@ contributors: - ["George Petrov", "http://github.com/petrovg"] - ["Dominic Bou-Samra", "http://dbousamra.github.com"] - ["Geoff Liu", "http://geoffliu.me"] + - ["Ha-Duong Nguyen", "http://reference-error.org"] filename: learn.scala --- @@ -33,10 +34,10 @@ Scala - the scalable language // 1. Basics ///////////////////////////////////////////////// -// Single line comments start with two forward slashes +// Single-line comments start with two forward slashes /* - Multi line comments, as you can already see from above, look like this. + Multi-line comments, as you can already see from above, look like this. */ // Printing, and forcing a new line on the next print -- cgit v1.2.3 From 8c06438ec9aec991b78bbd49d2343dc820da7669 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:49:15 +0700 Subject: scala: correct format --- 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 4121dc9b..03d80043 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -48,7 +48,7 @@ println(10) print("Hello world") // Declaring values is done using either var or val. -// val declarations are immutable, whereas var's are mutable. Immutability is +// val declarations are immutable, whereas vars are mutable. Immutability is // a good thing. val x = 10 // x is now 10 x = 20 // error: reassignment to val -- cgit v1.2.3 From 5b683b34ddb313b8981f360c4a6b54cfbc3af396 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:49:54 +0700 Subject: scala: easier-to-look comment --- scala.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 03d80043..0bd1a56d 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -51,9 +51,9 @@ print("Hello world") // val declarations are immutable, whereas vars are mutable. Immutability is // a good thing. val x = 10 // x is now 10 -x = 20 // error: reassignment to val +x = 20 // error: reassignment to val var y = 10 -y = 20 // y is now 20 +y = 20 // y is now 20 /* Scala is a statically typed language, yet note that in the above declarations, @@ -73,17 +73,17 @@ true false // Boolean operations -!true // false -!false // true +!true // false +!false // true true == false // false -10 > 5 // true +10 > 5 // true // Math is as per usual -1 + 1 // 2 -2 - 1 // 1 -5 * 3 // 15 -6 / 2 // 3 -6 / 4 // 1 +1 + 1 // 2 +2 - 1 // 1 +5 * 3 // 15 +6 / 2 // 3 +6 / 4 // 1 6.0 / 4 // 1.5 -- cgit v1.2.3 From dbdde5134b4c5777467a81c8fdf2128cb44edacc Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:50:32 +0700 Subject: scala: clearer comments --- scala.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 0bd1a56d..8d93169b 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -122,12 +122,12 @@ s"We have $n apples" // => "We have 45 apples" // Expressions inside interpolated strings are also possible val a = Array(11, 9, 6) -s"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old." +s"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old." s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples." -s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4" +s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4" // Formatting with interpolated strings with the prefix "f" -f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25" +f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25" f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122: 11.0454" // Raw strings, ignoring special characters. -- cgit v1.2.3 From efb338608b3b6e5af3652e71aa7fb85bdf5917b9 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:55:38 +0700 Subject: scala: consistent code format --- scala.html.markdown | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 8d93169b..62af31b6 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -173,8 +173,8 @@ def sq(x: Int) = x * x // Compiler can guess return type is Int // Functions can have default parameters: def addWithDefault(x: Int, y: Int = 5) = x + y -addWithDefault(1, 2) // => 3 -addWithDefault(1) // => 6 +addWithDefault(1, 2) // => 3 +addWithDefault(1) // => 6 // Anonymous functions look like this: @@ -195,8 +195,8 @@ sq(10) // => 100 val addOne: Int => Int = _ + 1 val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3) -addOne(5) // => 6 -weirdSum(2, 4) // => 16 +addOne(5) // => 6 +weirdSum(2, 4) // => 16 // The return keyword exists in Scala, but it only returns from the inner-most @@ -206,9 +206,9 @@ weirdSum(2, 4) // => 16 def foo(x: Int): Int = { val anonFunc: 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 foo! else - z + 2 // This line is the return value of anonFunc + z + 2 // This line is the return value of anonFunc } anonFunc(x) // This line is the return value of foo } @@ -361,7 +361,7 @@ class Dog(br: String) { val mydog = new Dog("greyhound") println(mydog.breed) // => "greyhound" -println(mydog.bark) // => "Woof, woof!" +println(mydog.bark) // => "Woof, woof!" // The "object" keyword creates a type AND a singleton instance of it. It is @@ -543,8 +543,8 @@ implicit def myImplicitFunction(breed: String) = new Dog("Golden " + breed) // By itself, implicit keyword doesn't change the behavior of the value, so // above values can be used as usual. -myImplicitInt + 2 // => 102 -myImplicitFunction("Pitbull").breed // => "Golden Pitbull" +myImplicitInt + 2 // => 102 +myImplicitFunction("Pitbull").breed // => "Golden Pitbull" // The difference is that these values are now eligible to be used when another // piece of code "needs" an implicit value. One such situation is implicit @@ -572,8 +572,8 @@ def foo[T : C] = ... // implicit conversion of type A => B, where A is the type of obj, and B has a // method called "method", that conversion is applied. So having // myImplicitFunction above in scope, we can say: -"Retriever".breed // => "Golden Retriever" -"Sheperd".bark // => "Woof, woof!" +"Retriever".breed // => "Golden Retriever" +"Sheperd".bark // => "Woof, woof!" // Here the String is first converted to Dog using our function above, and then // the appropriate method is called. This is an extremely powerful feature, but -- cgit v1.2.3 From 7c02089687eaee9c1fe9b1db3ab0674153077f56 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:55:59 +0700 Subject: scala: consistent code format --- scala.html.markdown | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 62af31b6..35042ebe 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -220,19 +220,19 @@ def foo(x: Int): Int = { 1 to 5 val r = 1 to 5 -r.foreach( println ) +r.foreach(println) 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 -(5 to 1 by -1) foreach ( println ) +(5 to 1 by -1) foreach (println) // A while loops var i = 0 -while (i < 10) { println("i " + i); i+=1 } +while (i < 10) { println("i " + i); i += 1 } -while (i < 10) { println("i " + i); i+=1 } // Yes, again. What happened? Why? +while (i < 10) { println("i " + i); i += 1 } // Yes, again. What happened? Why? i // Show the value of i. Note that while is a loop in the classical sense - // it executes sequentially while changing the loop variable. while is very @@ -241,19 +241,19 @@ i // Show the value of i. Note that while is a loop in the classical sense - // A do while loop do { - println("x is still less than 10"); + println("x is still less than 10") x += 1 } while (x < 10) // Tail recursion is an idiomatic way of doing recurring things in Scala. // Recursive functions need an explicit return type, the compiler can't infer it. // Here it's Unit. -def showNumbersInRange(a:Int, b:Int):Unit = { +def showNumbersInRange(a:Int, b:Int): Unit = { print(a) if (a < b) showNumbersInRange(a + 1, b) } -showNumbersInRange(1,14) +showNumbersInRange(1, 14) // Conditionals @@ -307,13 +307,13 @@ s(1) (a, 2, "three") // Why have this? -val divideInts = (x:Int, y:Int) => (x / y, x % y) +val divideInts = (x: Int, y: Int) => (x / y, x % y) -divideInts(10,3) // The function divideInts gives you the result and the remainder +divideInts(10, 3) // The function divideInts gives you the result and the remainder // To access the elements of a tuple, use _._n where n is the 1-based index of // the element -val d = divideInts(10,3) +val d = divideInts(10, 3) d._1 @@ -416,8 +416,8 @@ val otherGeorge = george.copy(phoneNumber = "9876") def matchPerson(person: Person): String = person match { // Then you specify the patterns: case Person("George", number) => "We found George! His number is " + number - case Person("Kate", number) => "We found Kate! Her number is " + number - case Person(name, number) => "We matched someone : " + name + ", phone : " + number + case Person("Kate", number) => "We found Kate! Her number is " + number + case Person(name, number) => "We matched someone : " + name + ", phone : " + number } val email = "(.*)@(.*)".r // Define a regex for the next example. @@ -448,7 +448,7 @@ def matchEverything(obj: Any): String = obj match { case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c" // You can nest patterns: - case List(List((1, 2,"YAY"))) => "Got a list of list of tuple" + case List(List((1, 2, "YAY"))) => "Got a list of list of tuple" } // In fact, you can pattern match any object with an "unapply" method. This @@ -495,7 +495,7 @@ sSquared.reduce (_+_) // The filter function takes a predicate (a function from A -> Boolean) and // selects all elements which satisfy the predicate List(1, 2, 3) filter (_ > 2) // List(3) -case class Person(name:String, age:Int) +case class Person(name: String, age: Int) List( Person(name = "Dom", age = 23), Person(name = "Bob", age = 30) @@ -596,7 +596,7 @@ import scala.collection.immutable._ import scala.collection.immutable.{List, Map} // Rename an import using '=>' -import scala.collection.immutable.{ List => ImmutableList } +import scala.collection.immutable.{List => ImmutableList} // Import all classes, except some. The following excludes Map and Set: import scala.collection.immutable.{Map => _, Set => _, _} -- cgit v1.2.3 From 513235c4754f87e64e5d564bb53ec643ce9333df Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:56:11 +0700 Subject: scala: clearer further resources (itemized) --- scala.html.markdown | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 35042ebe..10db5f78 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -631,13 +631,8 @@ writer.close() ## Further resources -[Scala for the impatient](http://horstmann.com/scala/) - -[Twitter Scala school](http://twitter.github.io/scala_school/) - -[The scala documentation](http://docs.scala-lang.org/) - -[Try Scala in your browser](http://scalatutorials.com/tour/) - -Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user) - +* [Scala for the impatient](http://horstmann.com/scala/) +* [Twitter Scala school](http://twitter.github.io/scala_school/) +* [The scala documentation](http://docs.scala-lang.org/) +* [Try Scala in your browser](http://scalatutorials.com/tour/) +* Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user) -- cgit v1.2.3 From d61ad107759c99de3dd32dc848086c16dbb14f15 Mon Sep 17 00:00:00 2001 From: "Ha-Duong, NGUYEN" Date: Mon, 3 Aug 2015 15:58:36 +0700 Subject: scala: consistent code format --- 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 10db5f78..c482752d 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -178,7 +178,7 @@ addWithDefault(1) // => 6 // Anonymous functions look like this: -(x:Int) => x * x +(x: Int) => x * x // Unlike defs, even the input type of anonymous functions can be omitted if the // context makes it clear. Notice the type "Int => Int" which means a function @@ -248,7 +248,7 @@ do { // Tail recursion is an idiomatic way of doing recurring things in Scala. // Recursive functions need an explicit return type, the compiler can't infer it. // Here it's Unit. -def showNumbersInRange(a:Int, b:Int): Unit = { +def showNumbersInRange(a: Int, b: Int): Unit = { print(a) if (a < b) showNumbersInRange(a + 1, b) -- cgit v1.2.3 From f30200b31c60f082cf4f192245a4a2c058f76fa4 Mon Sep 17 00:00:00 2001 From: Kaleb Davis Date: Tue, 6 Oct 2015 21:22:09 -0400 Subject: Add examples to show how printing works --- scala.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index c482752d..7189be10 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -43,9 +43,13 @@ Scala - the scalable language // Printing, and forcing a new line on the next print println("Hello world!") println(10) +// Hello world! +// 10 // Printing, without forcing a new line on next print print("Hello world") +print(10) +// Hello world!10 // Declaring values is done using either var or val. // val declarations are immutable, whereas vars are mutable. Immutability is -- cgit v1.2.3 From 7c2c4480600b6c817418d5ba04315bca3c5a400d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Costa?= Date: Sat, 10 Oct 2015 16:49:53 +0100 Subject: Fix compile errors of the English and French Scala tutorials --- scala.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 7189be10..7f545196 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -6,7 +6,6 @@ contributors: - ["Dominic Bou-Samra", "http://dbousamra.github.com"] - ["Geoff Liu", "http://geoffliu.me"] - ["Ha-Duong Nguyen", "http://reference-error.org"] -filename: learn.scala --- Scala - the scalable language @@ -244,10 +243,11 @@ i // Show the value of i. Note that while is a loop in the classical sense - // comprehensions above is easier to understand and parallelize // A do while loop +i = 0 do { - println("x is still less than 10") - x += 1 -} while (x < 10) + println("i is still less than 10") + i += 1 +} while (i < 10) // Tail recursion is an idiomatic way of doing recurring things in Scala. // Recursive functions need an explicit return type, the compiler can't infer it. @@ -566,8 +566,8 @@ sendGreetings("Jane") // => "Hello Jane, 100 blessings to you and yours!" // Implicit function parameters enable us to simulate type classes in other // functional languages. It is so often used that it gets its own shorthand. The // following two lines mean the same thing: -def foo[T](implicit c: C[T]) = ... -def foo[T : C] = ... +// def foo[T](implicit c: C[T]) = ... +// def foo[T : C] = ... // Another situation in which the compiler looks for an implicit is if you have -- cgit v1.2.3 From 069e127ff83820ba809bc437bfadda9839d91062 Mon Sep 17 00:00:00 2001 From: chris-hranj Date: Sat, 24 Oct 2015 23:53:17 -0400 Subject: update Data Structures section to show outputs --- scala.html.markdown | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 7f545196..192e03d7 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -278,21 +278,21 @@ val text = if (x == 10) "yeah" else "nope" ///////////////////////////////////////////////// val a = Array(1, 2, 3, 5, 8, 13) -a(0) -a(3) +a(0) // Int = 1 +a(3) // Int = 5 a(21) // Throws an exception val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo") -m("fork") -m("spoon") +m("fork") // java.lang.String = tenedor +m("spoon") // java.lang.String = cuchara m("bottle") // Throws an exception val safeM = m.withDefaultValue("no lo se") -safeM("bottle") +safeM("bottle") // java.lang.String = no lo se val s = Set(1, 3, 7) -s(0) -s(1) +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 @@ -313,15 +313,16 @@ s(1) // Why have this? val divideInts = (x: Int, y: Int) => (x / y, x % y) -divideInts(10, 3) // The function divideInts gives you the result and the remainder +// The function divideInts gives you the result and the remainder +divideInts(10, 3) // (Int, Int) = (3,1) // To access the elements of a tuple, use _._n where n is the 1-based index of // the element -val d = divideInts(10, 3) +val d = divideInts(10, 3) // (Int, Int) = (3,1) -d._1 +d._1 // Int = 3 -d._2 +d._2 // Int = 1 ///////////////////////////////////////////////// -- cgit v1.2.3 From bc087b50370a3c32c35ef026047c2fa9db9944d8 Mon Sep 17 00:00:00 2001 From: Pavel Kazlou Date: Sat, 31 Oct 2015 16:39:47 +0300 Subject: usage of named parameters --- scala.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 192e03d7..bc8cd422 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -169,6 +169,12 @@ def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y // Syntax for calling functions is familiar: sumOfSquares(3, 4) // => 25 +// You can use parameters names to specify them in different order +def subtract(x: Int, y: Int): Int = x - y + +subtract(10, 3) // => 7 +subtract(y=10, x=3) // => -7 + // In most cases (with recursive functions the most notable exception), function // return type can be omitted, and the same type inference we saw with variables // will work with function return values: -- cgit v1.2.3