diff options
Diffstat (limited to 'scala.html.markdown')
| -rw-r--r-- | scala.html.markdown | 117 | 
1 files changed, 89 insertions, 28 deletions
| diff --git a/scala.html.markdown b/scala.html.markdown index 7f545196..5e3ece2d 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -12,22 +12,62 @@ Scala - the scalable language  ```scala +///////////////////////////////////////////////// +// 0. Basics +/////////////////////////////////////////////////  /* -  Set yourself up: +  Setup Scala:    1) Download Scala - http://www.scala-lang.org/downloads    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 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. -  */ +/* +  Try the REPL +   +  Scala has a tool called the REPL (Read-Eval-Print Loop) that is anologus to  +  commandline interpreters in many other languages. You may type any Scala +  expression, and the result will be evaluated and printed.   +   +  The REPL is a very handy tool to test and verify code.  Use it as you read  +  this tutorial to quickly explore concepts on your own. +*/ +   +// Start a Scala REPL by running `scala`. You should see the prompt: +$ scala +scala> + +// By default each expression you type is saved as a new numbered value +scala> 2 + 2 +res0: Int = 4 + +// Default values can be reused.  Note the value type displayed in the result.. +scala> res0 + 2 +res1: Int = 6 + +// Scala is a strongly typed language. You can use the REPL to check the type +// without evaluating an expression. +scala> :type (true, 2.0) +(Boolean, Double) + +// REPL sessions can be saved  +scala> :save /sites/repl-test.scala + +// Files can be loaded into the REPL +scala> :load /sites/repl-test.scala +Loading /sites/repl-test.scala... +res2: Int = 4 +res3: Int = 6 + +// You can search your recent history  +scala> :h? +1 2 + 2 +2 res0 + 2 +3 :save /sites/repl-test.scala +4 :load /sites/repl-test.scala +5 :h? + +// Now that you know how to play, let's learn a little scala...  /////////////////////////////////////////////////  // 1. Basics @@ -48,7 +88,7 @@ println(10)  // Printing, without forcing a new line on next print  print("Hello world")  print(10) -// Hello world!10 +// Hello world10  // Declaring values is done using either var or val.  // val declarations are immutable, whereas vars are mutable. Immutability is @@ -88,6 +128,7 @@ true == false // false  6 / 2   // 3  6 / 4   // 1  6.0 / 4 // 1.5 +6 / 4.0 // 1.5  // Evaluating an expression in the REPL gives you the type and value of the result @@ -169,6 +210,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: @@ -231,7 +278,7 @@ r foreach println  (5 to 1 by -1) foreach (println) -// A while loops +// A while loop  var i = 0  while (i < 10) { println("i " + i); i += 1 } @@ -239,17 +286,18 @@ 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 -     // fast, faster that Java loops, but using the combinators and -     // comprehensions above is easier to understand and parallelize +     // fast, but using the combinators and comprehensions above is easier +     // to understand and parallelize -// A do while loop +// A do-while loop  i = 0  do {    println("i is still less than 10")    i += 1  } while (i < 10) -// Tail recursion is an idiomatic way of doing recurring things in Scala. +// 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.  def showNumbersInRange(a: Int, b: Int): Unit = { @@ -267,7 +315,7 @@ val x = 10  if (x == 1) println("yeah")  if (x == 10) println("yeah")  if (x == 11) println("yeah") -if (x == 11) println ("yeah") else println("nay") +if (x == 11) println("yeah") else println("nay")  println(if (x == 10) "yeah" else "nope")  val text = if (x == 10) "yeah" else "nope" @@ -278,21 +326,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 +361,22 @@ 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    // Int = 1 -d._2 +// Alternatively you can do multiple-variable assignment to tuple, which is more +// convenient and readable in many cases +val (div, mod) = divideInts(10, 3) + +div     // Int = 3 +mod     // Int = 1  ///////////////////////////////////////////////// @@ -453,6 +508,9 @@ def matchEverything(obj: Any): String = obj match {    // You can nest patterns:    case List(List((1, 2, "YAY"))) => "Got a list of list of tuple" + +  // Match any case (default) if all previous haven't matched +  case _ => "Got unknown object"  }  // In fact, you can pattern match any object with an "unapply" method. This @@ -605,6 +663,9 @@ import scala.collection.immutable.{List => ImmutableList}  // Import all classes, except some. The following excludes Map and Set:  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  // single method, main:  object Application { | 
