Getting Started with Symbols in Scala

Spark is written in Scala. Spark, as a very easy-to-use and efficient big data framework, is more and more widely used, and more and more people are learning Scala.
The language is similar, and I believe that programmers with python and java foundations will not have much difficulty in learning Scala. But the weird operators in Scala programs can make you scratch your head.
Most of the Scala tutorials start with variable definitions, functions, classes, etc. We start directly with Scala symbols and experience the features of Scala directly from the code.

1. Strange Charm<-

Often used in for loops, it is called a generator.

During the execution process, the elements in the collection filesHere (Array[File]) will be assigned to file in turn, and the file type is File, and its toString method is called to print the file name when printing.

val filesHere = (new java.io.File(".")).listFiles
//直接遍历数组元素
for (file <- filesHere)
     | println(file)

//间接遍历数组
for (i <- 0 to filesHere.length - 1)
println(filesHere(i))

You can also cooperate until, ifetc. to add some restrictions.

for(i <- res39) println("Iteration"+i)

for(i <- 1 to 5) println("Iteration"+i)

// 这样写看着奇怪,是同楼上一样的
for(i <- 1 .to(5)) println("Iteration"+i)

//如果不需要5出现,则用until
scala> for(i <- 1 until 5) println("Iteration"+i)

//加入if条件
for (file <- filesHere if file.getName.endsWith(".scala"))
println(file)

2. Strange Charm->

This notation is used for Map. Map is an iterable key/value pair structure. Scala uses immutable Maps by default. If you want to use a mutable collection, you need to explicitly import the import scala.collection.mutable.Map class

// 空哈希表,键为字符串,值为整型
var A:Map[Char,Int] = Map()

// 需要添加 key/value对,可以使用 + 号
A += ('I' -> 1)
A += ('J' -> 5)
A += ('K' -> 10)
A += ('L' -> 100)

// Map 键值对演示
val B = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")

//使用 ++ 运算符或 Map.++() 方法来连接两个 Map,Map 合并时会移除重复的 key
//  ++ 作为运算符
var ab = A ++ B
//  ++ 作为方法
ab = A.++(B)

3. Strange Charm=>

Similar to conversion notation, => specifies that this function converts the thing on the left (any integer x) to the thing on the right (x + 1). So, this is a function that maps any integer x to x + 1. Kind of like an anonymous function, an anonymous function consists of a parameter list, arrow connectors, =>and a function body.

// 定义匿名函数
val cube = (x: Int) => x * x *x 

(x: Int) is the parameter of the anonymous function, x * x *x is the function body, and the arrow connector
=>connects the parameter list and the function body.

Symbols in Scala =>can also be seen as syntactic sugar for creating function instances. For example: A => T, A,B => Tindicating that the input parameter type of a function is "A", "A, B", and the return value type is T.

val f: Int => String = myInt => "The value of myInt is: " + myInt.toString()
println(f(3))

The above example defines the function f: the input parameter is an integer type, and the return value is a string.

Also, () => T means that the function input parameter is empty, while A => Unit means that the function has no return value.

4. Strange Charm_

This is the same as python, a variable that programmers are too lazy to name. Sometimes it is also used as a wildcard.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325301992&siteId=291194637