SCALA Study Notes (1), SCALA Study Notes (
Variable
When obtaining the variable value is time-consuming, you can use lazy var.
Lazy val forLater = someTimeConsumingOperation ()
Scala> val first: rest = List (1, 2, 3)
First: Int = 1
Rest: List [Int] = List (2, 3)
Function Definition
"=" Is not just used to separate the function signature and function body. It also serves to tell the compiler whether to deduce the type of the function return value!If = is omitted, no return value is returned for the function!
For example:
Scala> def myFirstMethod () = {"exciting times ahead "}
MyFirstMethod: () java. lang. String
Scala> def myFirstMethod () {"exciting times ahead "}
MyFirstMethod: () Unit
FUNCTION literal: FUNCTION LITERALS
In Scala you can also pass a function as a parameter to another function, and most of the time in those cases I provide an inline definition of the function. this passing of functions as a parameter is sometimes loosely called closure (passing a function isn' t always necessarily closure; you'll look into that in chapter 4 ). scala provides a shorthand way to create a function in which you write only the function body, called function literals.
Simply put, the function literal is to save the function signature (mainly the function name and parameter Declaration) and leave only a piece of code in the function body! Note: This is different from anonymous functions. Anonymous functions only have no function name, But parameter declaration will not be omitted! For the function literal, because parameter declaration is omitted, when a parameter is required in the function body, an underscore is used to represent it! This is also a feature of the function literal!
For example:
// Obviously, part of the curly braces is an anonymous function scala> evenNumbers. foldLeft (0) {(a: Int, B: Int) => a + B} // The part in curly braces is still an anonymous function, just saves the parameter type declaration scala> evenNumbers. foldLeft (0) {(a, B) => a + B} // some substantial changes have occurred when this version was evolved: // The parameter Declaration is also omitted, obviously, we can no longer call it an anonymous function! // With the cancellation of the parameter declaration, a problem arises: how can we refer to and Mark parameters in the function body? Scala> evenNumbers. foldLeft (0) {_ + _}
In scala, an underline represents a parameter!
Closure: Closure
A closure is any function that closes over the environment in which it's defined. For example, closure will keep track of any variable changes outside the function that are being referred to inside the function.
Def breakable (op: => Unit ){... }
What's this op: => Unit? The special right arrow (=>) lets Scala know that the breakable function expects a function as a parameter. the right side of the => defines the return type of the function-in this case it's Unit (similar to Java void)-and op is the name of the parameter. because you haven't specified anything on the left side of the arrow, it means that the function you're expecting as a parameter doesn't take any parameter for itself.
What if the required input parameter is a substitute parameter with a return value? How should such a function be described as a parameter?
Def foldLeft (initialValue: Int, operator: (Int, Int) => Int) = {... }
Let's take a look at this: since a function is the first class in a functional programming language, it can be used like other data types, when it is used as a function parameter, we need to define a syntax to describe this "parameter" (actually a function) (In fact, it should be the "meta information" of this function). How should we describe the "type" of a function? From the perspective of the language designer, the most reasonable way to describe is to state the parameter type and Return Value Type of the function! This is exactly what operator: (Int, Int) => Int) does! Simple, clear, and reasonable!
Array
scala> val array = new Array[String](3)array: Array[String] = Array(null, null, null)scala> array(0) = "This"scala> array(1) = "is"scala> array(2) = "mutable"
For statements that assign values to an array: array (0) = "This", it should be noted that: Unlike array [0] = "This" in java, [] is always used to set the parameter type!
FOR Loop
val files = new java.io.File(".").listFiles for(file <- files) { val filename = file.getName if(fileName.endsWith(".scala")) println(file)}
The only thing that looks different from for loops in Java or C # is the expression file <-files. in Scala this is called a generator, and the job of a generator is to iterate through a collection.
scala> val aList = List(1, 2, 3)aList: List[Int] = List(1, 2, 3)scala> val bList = List(4, 5, 6)bList: List[Int] = List(4, 5, 6)scala> for { a <- aList; b <- bList } println(a + b)567678789 scala> val result = for { a <- aList; b <- bList } yield a + bresult: List[Int] = List(5, 6, 7, 6, 7, 8, 7, 8, 9)scala> for(r <- result) println(r)567678789
Note: The braces {} of the for statement are not mandatory. You can use ().
Pattern Match: Pattern Matching
Pattern matching, Example 1:
ordinal(args(0).toInt)def ordinal(number:Int) = number match { case 1 => println("1st") case 2 => println("2nd") case 3 => println("3rd") case 4 => println("4th") case 5 => println("5th") case 6 => println("6th") case 7 => println("7th") case 8 => println("8th") case 9 => println("9th") case 10 => println("10th") case _ => println("Cannot do beyond 10")}
Case _ to match everything else.
Pattern matching, Example 2:
The following example shows some predefined Pattern of scala, which is specially applied to case. For example, f, s, and rest in the following example.
scala> List(1, 2, 3, 4) match {case f :: s :: rest => List(f, s)case _ => Nil}res7: List[Int] = List(1, 2)
Pattern matching, Example 3:
val suffixes = List("th", "st", "nd", "rd", "th", "th", "th","th", "th","th");println(ordinal(args(0).toInt))def ordinal(number:Int) = number match { case tenTo20 if 10 to 20 contains tenTo20 => number + "th" case rest => rest + suffixes(number % 10)}
Class: Class
What val and var do is define a field and a getter for that field, and in the case of var an additional setter method is also created. when both of them are missing, they're treated as private instance values, not accessible to anyone outside the class.
The field modifier of Class has the following conventions: