diff options
Diffstat (limited to 'scala.html.markdown')
| -rw-r--r-- | scala.html.markdown | 29 | 
1 files changed, 21 insertions, 8 deletions
| diff --git a/scala.html.markdown b/scala.html.markdown index bc8cd422..745605ed 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -48,7 +48,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 @@ -237,7 +237,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 } @@ -245,17 +245,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 = { @@ -273,7 +274,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" @@ -327,9 +328,15 @@ divideInts(10, 3)    // (Int, Int) = (3,1)  val d = divideInts(10, 3)    // (Int, Int) = (3,1)  d._1    // Int = 3 -  d._2    // Int = 1 +// 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 +  /////////////////////////////////////////////////  // 5. Object Oriented Programming @@ -460,6 +467,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 @@ -612,6 +622,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 { | 
