1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-12 01:34:19 +02:00

Merge pull request #1179 from cmpitg/master

[scala/en] Consistent code format & some minor language corrections
This commit is contained in:
ven
2015-08-03 11:18:26 +02:00

View File

@@ -5,6 +5,7 @@ contributors:
- ["George Petrov", "http://github.com/petrovg"] - ["George Petrov", "http://github.com/petrovg"]
- ["Dominic Bou-Samra", "http://dbousamra.github.com"] - ["Dominic Bou-Samra", "http://dbousamra.github.com"]
- ["Geoff Liu", "http://geoffliu.me"] - ["Geoff Liu", "http://geoffliu.me"]
- ["Ha-Duong Nguyen", "http://reference-error.org"]
filename: learn.scala filename: learn.scala
--- ---
@@ -16,15 +17,16 @@ Scala - the scalable language
Set yourself up: Set yourself up:
1) Download Scala - http://www.scala-lang.org/downloads 1) Download Scala - http://www.scala-lang.org/downloads
2) unzip/untar in your favourite location and put the bin subdir on the path 2) Unzip/untar to your favourite location and put the bin subdir in your `PATH` environment variable
3) Start a scala REPL by typing scala. You should see the prompt: 3) Start a Scala REPL by running `scala`. You should see the prompt:
scala> scala>
This is the so called REPL (Read-Eval-Print Loop). You may type any valid This is the so called REPL (Read-Eval-Print Loop). You may type any Scala
Scala expression into it, and the result will be printed. We will explain what expression, and the result will be printed. We will explain what Scala files
Scala files look like further into this tutorial, but for now, let's start look like further into this tutorial, but for now, let's start with some
with some basics. basics.
*/ */
@@ -32,10 +34,10 @@ Scala - the scalable language
// 1. Basics // 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 // Printing, and forcing a new line on the next print
@@ -46,7 +48,7 @@ println(10)
print("Hello world") print("Hello world")
// Declaring values is done using either var or val. // 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. // a good thing.
val x = 10 // x is now 10 val x = 10 // x is now 10
x = 20 // error: reassignment to val x = 20 // error: reassignment to val
@@ -176,7 +178,7 @@ addWithDefault(1) // => 6
// Anonymous functions look like this: // 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 // 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 // context makes it clear. Notice the type "Int => Int" which means a function
@@ -218,19 +220,19 @@ def foo(x: Int): Int = {
1 to 5 1 to 5
val r = 1 to 5 val r = 1 to 5
r.foreach( println ) r.foreach(println)
r foreach println 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
(5 to 1 by -1) foreach ( println ) (5 to 1 by -1) foreach (println)
// A while loops // A while loops
var i = 0 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 - 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 // it executes sequentially while changing the loop variable. while is very
@@ -239,19 +241,19 @@ i // Show the value of i. Note that while is a loop in the classical sense -
// A do while loop // A do while loop
do { do {
println("x is still less than 10"); println("x is still less than 10")
x += 1 x += 1
} while (x < 10) } while (x < 10)
// Tail recursion is an idiomatic way of doing recurring things in Scala. // 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. // Recursive functions need an explicit return type, the compiler can't infer it.
// Here it's Unit. // Here it's Unit.
def showNumbersInRange(a:Int, b:Int):Unit = { def showNumbersInRange(a: Int, b: Int): Unit = {
print(a) print(a)
if (a < b) if (a < b)
showNumbersInRange(a + 1, b) showNumbersInRange(a + 1, b)
} }
showNumbersInRange(1,14) showNumbersInRange(1, 14)
// Conditionals // Conditionals
@@ -305,13 +307,13 @@ s(1)
(a, 2, "three") (a, 2, "three")
// Why have this? // 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 // To access the elements of a tuple, use _._n where n is the 1-based index of
// the element // the element
val d = divideInts(10,3) val d = divideInts(10, 3)
d._1 d._1
@@ -446,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" case List(1, b, c) => s"Got a list with three elements and starts with 1: 1, $b, $c"
// You can nest patterns: // 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 // In fact, you can pattern match any object with an "unapply" method. This
@@ -493,7 +495,7 @@ sSquared.reduce (_+_)
// The filter function takes a predicate (a function from A -> Boolean) and // The filter function takes a predicate (a function from A -> Boolean) and
// selects all elements which satisfy the predicate // selects all elements which satisfy the predicate
List(1, 2, 3) filter (_ > 2) // List(3) List(1, 2, 3) filter (_ > 2) // List(3)
case class Person(name:String, age:Int) case class Person(name: String, age: Int)
List( List(
Person(name = "Dom", age = 23), Person(name = "Dom", age = 23),
Person(name = "Bob", age = 30) Person(name = "Bob", age = 30)
@@ -594,7 +596,7 @@ import scala.collection.immutable._
import scala.collection.immutable.{List, Map} import scala.collection.immutable.{List, Map}
// Rename an import using '=>' // 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 all classes, except some. The following excludes Map and Set:
import scala.collection.immutable.{Map => _, Set => _, _} import scala.collection.immutable.{Map => _, Set => _, _}
@@ -629,13 +631,8 @@ writer.close()
## Further resources ## Further resources
[Scala for the impatient](http://horstmann.com/scala/) * [Scala for the impatient](http://horstmann.com/scala/)
* [Twitter Scala school](http://twitter.github.io/scala_school/)
[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/)
[The scala documentation](http://docs.scala-lang.org/) * Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user)
[Try Scala in your browser](http://scalatutorials.com/tour/)
Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user)