diff options
Diffstat (limited to 'scala.html.markdown')
| -rw-r--r-- | scala.html.markdown | 54 | 
1 files changed, 37 insertions, 17 deletions
| diff --git a/scala.html.markdown b/scala.html.markdown index c482752d..a82983a5 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 @@ -43,9 +42,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 @@ -166,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: @@ -240,10 +249,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. @@ -274,21 +284,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 @@ -309,15 +319,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  ///////////////////////////////////////////////// @@ -449,6 +466,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 @@ -562,8 +582,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 | 
