catalogue
- Scala special symbol meaning
- 1. :::
- 2. ::
- 3. =>
- 4.: + and +:
- 5. ++
- 6. <-
- 7. ->
- 8. ++=
- 9. _
- 10. +=
- 11. -=
- 3.1 value
- 3.2 type
- 9.1 wildcards
- 9.2 refers to each element in the set
- 9.3 get the element value of the specified subscript in the tuple
- 9.4 group members that can be used to obtain tuples using pattern matching
- 9.5 represents the default value of a certain type
- 9.6 :_* As a whole, tell the compiler that you want to treat a parameter as a sequence of numbers
text
date: 2019-08-01 11:15:27
updated: 2019-08-22 15:22:32
Scala special symbol meaning
1. :::
::: (three colons) is only used to connect two collections of type List
val a = List(1, 2) val b = List(3, 4) val c = a ::: b
2. ::
:: (two colons) indicates the connection operation between ordinary elements and List
val a = 1 val b = List(3, 4) val c = 1 :: b
:: is the method of the object on the right, that is, it is the method of object b, and the operand on the left is the parameter of:: method, so 1::b means b.::(1)
3. =>
According to its different mathematical meanings, it has different meanings
3.1 value
Anonymous function or lamba expression, definition format: (parameter list) = > function body
(x: Int) => x + 1
3.2 type
Syntax sugar: simplify the required parameters, types and functions, and let the compiler parse (de sugar) them
the type Int => String, is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String.
val a: Int => String = myint => "my int : " + myint.toString; val b: (Int, Int) => String = (my1, my2) => "my int : " + my1.toString; var c = (x: Int, y:Int) => "my int : " + x + y def d(x: Int): String = { "my:" + x; }
The final calling methods of the above four methods are: A / B / C / D (parameter)
Where a b c is equivalent to giving a function body on the variable, and d itself is a function
b has two parameters. The call must have two parameters of Int type
4.: + and +:
: + method: used to append elements to the tail
+: Method: used to append an element to the header
The colon is always close to the collection type
scala> List(1,2,3) res1: List[Int] = List(1, 2, 3) scala> res1:+2 res2: List[Int] = List(1, 2, 3, 2) scala> 3+:res1 res4: List[Int] = List(3, 1, 2, 3)
5. ++
Used to connect two Seq, eg: list, set, string, map, etc
scala> val a = List(1,2,3) a: List[Int] = List(1, 2, 3) scala> a++List(4,5,6) res7: List[Int] = List(1, 2, 3, 4, 5, 6)
6. <-
For collection traversal
var arr = Array(1,2,3,4) for (i <- arr) { println(i) } 1 2 3 4
7. ->
Used to return a binary group
scala> var a = List(1,2,3) a: List[Int] = List(1, 2, 3) scala> var b = List(4,5,6) b: List[Int] = List(4, 5, 6) scala> val c = a -> b c: (List[Int], List[Int]) = (List(1, 2, 3),List(4, 5, 6))
8. ++=
For string splicing, + = adds elements to the variable array, which can be used to add values or splice strings, depending on the type on the left of the equation, but + + = can only splice strings
scala> var s:String = "a" s: String = a scala> s += "b" scala> println(s) ab scala> s ++= "c" scala> println(s) abc
9. _
9.1 wildcards
- _ Wildcards that can act like *:
import org.apache.spark.SparkContext._ // Import all jar packages
- pattern matching
Wildcards that represent completely ambiguous values
object MatchTest extends App { def matchTest(x: Int): String = x match { case 1 => "one" case 2 => "two" case _ => "many" } println(matchTest(3)) }
9.2 refers to each element in the set
Traverse the collection to filter elements in the list that are greater than a certain value val lst = List(1,2,3,4,5) val lstFilter = lst.filter(_ > 3)
As placeholders for parameters, the compiler may not have enough information to infer the missing parameter types.
val f = _ + _
In this case, you can use a colon to specify the type
val f = (_: Int) + (_: Int)
9.3 get the element value of the specified subscript in the tuple
val ss = (1,"22","333") println(ss._1)
9.4 group members that can be used to obtain tuples using pattern matching
val m = Map(1 -> 2,2 -> 4) for ((k,_) <- m) println(k) //If all parts are not required, use in the parts that are not required; This example only takes the key, so it is used at value_
9.5 represents the default value of a certain type
With or without spaces
For Int type, it is 0
For Double, it is 0.0
It is null for the reference type
var s: Int = _ def main(args: Array[String]): Unit = { println(s) }
9.6 :_* As a whole, tell the compiler that you want to treat a parameter as a sequence of numbers
def main(args: Array[String]): Unit = { val s = sum(1 to 5 :_*) //Treat 1 to 5 as a sequence println(s) } def sum(args: Int*) : Int = { var result = 0 ; for(s2 <- args) { result += s2 ; } result ; }
10. +=
Adding elements to a mutable array
11. -=
Removes the corresponding value from the mutable array
var map = Map(1 -> 1,2 -> 2,3 ->3 ) map -= 1 result: scala.collection.immutable.Map[Int,Int] = Map(2 -> 2, 3 -> 3)