Master Scala quickly

 
import java.awt._  // 引入包内所有成员
 
def handler(evt: event.ActionEvent) {
    
     // java.awt.event.ActionEvent
  ...  // 因为引入了java.awt,所以可以省去前面的部分
}

import java.awt.{
    
    Color, Font}
 
// 重命名成员
import java.util.{
    
    HashMap => JavaHashMap}
 
// 隐藏成员
import java.util.{
    
    HashMap => _, _} // 引入了util包的所有成员,但是HashMap被隐藏了

Symbolic literal

Symbol type is generally used for quick comparison, such as Map type: Map<Symbol, Data>, according to a Symbol object, you can quickly query the corresponding Data, while the query efficiency of Map<String, Data> is much lower.

Reprinted: Understanding Symbol in scala

    for (a <- 1 to 10; b <- 1 until 10
         if a * b > 50
    ) {
    
    
      println(a + " , " + b)
    }
//IndexedSeq[Int]
    var retVal = for {
    
    a <- 1 to 10 if a != 3} yield a

In Scala, you can use the val statement to define functions, and the def statement to define methods.

def m(x: Int) = x + 2

val f = (x: Int) => x + 2

Insert picture description here

Higher order function, template

  // 函数 f 和 值 v 作为参数,而函数 f 又调用了参数 v
  def apply(f: Int => String, v: Int): String = f(v)

  def layout[A](x: A): String = "[" + x.toString() + "]"

Partial function

val logWithDateBound = log(date, _ : String)

Guess you like

Origin blog.csdn.net/TQCAI666/article/details/111918948