# Scala_ Common functions

1, If Else…

```  val x = 5
val s = if (x > 0) 1 else -1 // s type is Int
val m = if (x > 0) "positive" else -1 // Public super type Any with s type String and Int
// In Scala, each expression should have a value. If the else part is missing, the following statement is equivalent to: if (x > 0) 1 else(), which returns a null value. () indicates the Unit class
if (x > 0) 1
```

2, Cycle: While, For

① While loop: there is no break or continue statement in Scala. When you need to use the break function, you can: use Boolean control variables, return from nested functions, and use the break method of the Breaks object

```  var n = 0
while (n < 10) {
println(n)
n += 1
}
```

② For loop: for (i < - expression), let the variable i traverse all the values of the expression to the right of < -.

```  for (i <- 1 to 10) {
println(i)    // 1 2 3 4 5 6 7 8 9 10
}
for (i <- 1 until 10) {
println(i)    // 1 2 3 4 5 6 7 8 9
}
```

③ Advanced for loop: provides mu lt iple generators in the form of 'variable < - expression', separated by semicolons

```  for (i <- 1 to 3; j <- 1 to 3) print ((10 * i +j) + " ")   // 11 12 13 21 22 23 31 32 33

// Each generator can have a guard, a Boolean expression starting with if
for (i <- 1 to 3; j <- 1 to 3 if i != j) print ((10 * i + j) + " ")  // 12 13 21 23 31 32

// Introduce variables that can be used in loops
for (i <- 1 to 3; from = 4 - i; j <- from to 3) print ((10 * i + j) + " ")  // 13 22 23 31 32 33

// If the loop body of the for loop starts with yield, the loop constructs a set, generating a value in the set each iteration
// for derivation. The generated collection is type compatible with its first generator.
val v = for (i <- 1 to 10) yield i%3
println(v)   // Vector(1, 2, 0, 1, 2, 0, 1, 2, 0, 1)
```

3, Exception: exception
throw expressions have a special type Nothing, which is useful in if/else expressions. If the type of one branch is Nothing, the type of if/else expression is the type of the other branch

```  val q:Int = 10
if (q >= 0) {sqrt(q)} else throw new IllegalArgumentException("x should not be negative")
// The first branch type is Double, and the second branch type is Nothing. Therefore, the type of if/else expression is Double
```

4, Lazy
When val is declared lazy, its initialization will be delayed until we get its value for the first time.

```  lazy val words= scala.io.Source.fromFile("words.txt") .mkString
// Read the file and concatenate the contents into a string. Because of the lazy type, if words are not used in the program, the file will not be read
```

5, Regular
To construct a Regex object, just use the r method of String class
If a regular expression contains backslashes or quotes, it is best to use the original String Syntax: "" ""“

```  val numPattern = "[0-9]+".r
val wsnumwsPattern = """\s+[0-9]+\s""".r
// The findAllIn method returns an iterator that traverses all matches. You can use it in a for loop
for (matchString <- numPattern.findAllIn("lib\\idea_rt.jar=8077:G:\\ideaIU\\IntelliJ IDEA 2017.1.4\\b" +
"in\" -Dfile.encoding=UTF-8 -classpath D:\\Java\\jre\\lib\\chars")) println(matchString)
// To find the first match in a string, use findFirstIn
val x = numPattern.findFirstIn("lib\\idea_rt.jar=8077:G:\\ideaIU\\IntelliJ IDEA 2017.1.4\\b" +
"in\" -Dfile.encoding=UTF-8 -classpath D:\\Java\\jre\\lib\\chars")
println(x.mkString)   // 8077
// To check whether the beginning of a string can match, use findPrefixOf
val y = numPattern.findPrefixOf("lib\\idea_rt.jar=8077:G:\\ideaIU\\IntelliJ IDEA 2017.1.4\\b" +
"in\" -Dfile.encoding=UTF-8 -classpath D:\\Java\\jre\\lib\\chars")
println(y)   // None
// You can replace the first match, or all matches
numPattern.replaceFirstIn("lib\\idea_rt.jar=8077:G:\\ideaIU\\IntelliJ IDEA 2017.1.4\\b", "23")
//Regular expression group
val numitemPattern = "([0-9] +)([a-z] +)".r
val numitemPattern(num, item) = "99 bottles"
```

Posted on Fri, 15 May 2020 11:08:22 -0400 by mrjap1