scala: consistent code format

This commit is contained in:
Ha-Duong, NGUYEN 2015-08-03 15:55:59 +07:00
parent efb338608b
commit 7c02089687

View File

@ -220,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
@ -241,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
@ -307,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
@ -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" 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
@ -495,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)
@ -596,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 => _, _}