Special symbols commonly used in Scala

Keywords: Programming Scala Spark Java Apache

1. = > anonymous function

In Spark, a function is also an object that can be assigned to a variable.

Format of Spark's anonymous function definition:

==(parameter list) = > {function body}==

Therefore, the function of = > is to create an anonymous function instance.

For example: (X: int) = > x + 1

2. < - (set traversal)

Loop traversal, for example:

var list = Array(1,2,3,4)
for (aa <- list) {
  printf(aa+"   ")
}

3. + + = (string splicing)

var s:String = "a"
s+="b"
println(s)
s++="c"
println(s)

4.::: three colon operators and:: two colon operators

  • ::: three colon operators represent the join operation of list. (similar to list1.addall (List2) in Java)
  • :: two colon operators represent the join operation between common elements and list. (similar to the list1.add(A) operation in Java)

scala operation example:

val one = List(1,2,3)
val two = List(4,5,6)
val three = one ::: two
println(three.toString())

val four = 7 :: three

println(four.toString())

5. - > construct the nth element of tuple and "N access tuple"

Meaning of tuple in scala:

  • Tuples are lists of different types of value aggregation threads
  • By enclosing multiple values in parentheses, a tuple is represented

Tuples and arrays in scala are different: the data type of elements in an array must be the same, but the data type of tuples can be different.

Sample program:

val first = (1,2,3) // Defining ternary groups

val one = 1
val two = 2
val three = one -> two

println(three) // Structural binary Group

println(three._2) // Accessing the second value in a binary tuple

6. Use of "underline"

a. wildcard

_Wildcards that act like *:

import org.apache.spark.SparkContext._

b. Refers to every element in a collection For example, traversing an element in a collection filter list that is greater than a value.

val lst = List(1,2,3,4,5)
val lstFilter = lst.filter(_ > 3)

c. Gets the element value of the specified subscript in the tuple

val ss = (1,"22","333")
println(ss._1)

d. Use pattern matching to get members of tuples

val m = Map(1 -> 2,2 -> 4)
for ((k,_) <- m) println(k) //If you don't need all the parts, use ﹐ in this case, only the key is used, so use ﹐ in value_

e. Add default values for member variables instead of local variables

var s:Int=_
def main(args: Array[String]): Unit = {
    println(s)
}

7.: * 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 ;
}

8. +=

Adding elements to a variable array

val arrBuf1 = new ArrayBuffer[Int]()
arrBuf1+= 11 // Add an element

println(arrBuf1)

9. -=

Remove the corresponding value from the map latter variable array

val arrBuf1 = new ArrayBuffer[Int]()
arrBuf1+= 11 // Add an element
arrBuf1+= 12 // Add an element
arrBuf1-= 12 // Delete an element

println(arrBuf1)

var map = Map(1 -> 1,2 -> 2,3 ->3 )
map-=1
println(map)

10.: + and +: (add elements to the list)

  • : + method: used to append elements at the end
  • +: Method: used to append elements to the header
  • Just remember that 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)

11. + + (connect two Seq)

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)

12. < (Generic upper bound)

def biophony[T <: Animal](things: Seq[T]) = things map (_.sound)

T <: Animal means: t must be a subclass of animal (including animal)

13. >: (lower bound of generics)

def biophony[T >: Animal](things: Seq[T]) = things map (_.sound)

Just like the upper bound of generics, only the passed in parameter must be the parent class of Animal

Posted by ctimmer on Thu, 12 Mar 2020 04:54:52 -0700