Scalar Basic Command Line
Table of Contents:
While Loop
- Method 01: Common while loop
- Method 02: Switch while loop to the end of doing (do-while)
For Loop
Note: \ We can use
1.to(5)
,1 until 6
, or1.until(6)
for represent range of number from 1 to 5.\ Optional, if you want to step this range, you can useby
likefor(i<-1 to 10 by 2)
.
- Loop
- Using
List
- Pass for loop into val
Break
import scala.util.control.Breaks._
// Breakable method to avoid exception
breakable {
for (i <- 1 to 10 by 2) {
if (i == 7)
break
else
println("i using to " + i)
}
}
Pattern Matching
def search(a: Any): Any = a match {
case 1 => println("One");
case "Two" => println("Two");
case "Hello" => println("Hello");
case _ => println("No");
}
val i = 7;
i match {
case 1 | 3 | 5 | 7 | 9 => println("odd");
case 2 | 4 | 6 | 8 => println("even");
}
Function
Note: \ You can ignore to write
return
in last line of function body that will auto use the last line to return.
object Math {
def square(x: Int) = x * x;
}
// Use method function of object with space seperator
println(Math square 3);
- Recursion Function
// We can set default value to `y` parameter
def plusRecursion(x: Int, y: Int = 40): Int = {
if (y == 0)
0
else
x + plusRecursion(x, y - 1)
}
Higher-Order Function
- Passing a Function as Parameter
Note: \ You can use wild-card like
math(50, 20, 10, _ + _);
- Function Composition
- Anonymous (lambda) Function
- Multiline Expression
- Partially Applied Function
- Closures
var number = 10;
val add = (x: Int) => x + number;
def main(args: Array[String]) {
number = 100;
println(add(20)); // stdout will show 120
}
val add = (x: Int) => {
number = x + number;
number;
}
def main(args: Array[String]) {
number = 100;
println(add(20)); // stdout will show 120
println(number); // stdout will show 120
}
- Function Currying
Note: \ You can write with oneline like
def add (x: Int) = (y: Int) => x + y;
, and Change to use withval add20 = add(20)
- Nested Function
def add(x: Int, y: Int, z: Int) = {
def addTwo(a: Int, b: Int) = {
a + b;
}
addTwo(x, addTwo(y, z));
}
- Function with Variable Length Parameters
String
str1.length(); // 11
str1.concat(str2); // Hello World Max
str1 + str2; // Hello World Max
str1.equals(str2); // false
str1.compareTo(str2); // -7
str1.substring(0, 5); // Hello
- String Interpolation
var s1 = "Scala string example";
var version = 2.12;
println(f"This is $s1%s, scala version is $version%2.2f"); // This is Scala string example, scala version is 2.12
Option
lst.find(_ > 2).get; // 3
map.get(1).get; // Tom
map.get(3).getOrElse("No name found"); // No name found
Exception
- Try Catch
class ExceptionExample {
def divide(a: Int, b: Int) = {
try {
a / b;
var arr = Array(1,2);
arr(10);
} catch {
case e: ArithmeticException => println(e)
case ex: Throwable => println("found a unknown exception: " + ex)
} finally {
println("Finally block always executes")
}
println("Rest of the code is executing...");
}
}
var e = new ExceptionExample();
e.divide(100, 0);
e.divide(100,10)
java.lang.ArithmeticException: / by zero
Finally block always executes
Rest of the code is executing...
found a unknown exception: java.lang.ArrayIndexOutOfBoundsException: 10
Finally block always executes
Rest of the code is executing...
- Throw keyword