Scala Pattern Match之Sequences && Tuples

Scala Pattern Match之Sequences && Tuples函數


模式匹配概述

val bools = Seq(true, false)

for (bool <- bools) {
  bool match {
    case true => println("Got heads")
    case false => println("Got tails")
  }
}

for {x <- Seq(1, 2, 2.7, "one", "two", "four")} {
  val str = x match {
    case 1 => "int 1"
    // 匹配 Int 類型
    case i: Int => "other int:" + i
    // 匹配Double類型
    case d: Double => "a double :" + x
    case "one" => "string one"
    // 匹配 String 類型
    case s: String => "other string:" + s
    // 上面沒有匹配到的,就會在這裏處理
    case unexpected => "unexpected value :" + unexpected
  }
  println(str)
}


/**
  * 使用下劃線 _ 表明把匹配的結果綁定爲該變量,用下劃線來表示。
  */
for {x <- Seq(1, 2, 2.7, "one", "two", "four")} {
  val str = x match {
    case 1 => "int 1"
    // 匹配 Int 類型
    case _: Int => "other int:" + x
    // 匹配Double類型
    case _: Double => "a double :" + x
    case "one" => "string one"
    // 匹配 String 類型
    case _: String => "other string:" + x
    // 上面沒有匹配到的,就會在這裏處理
    case _ => "unexpected value :" + x
  }
  println(str)
}

/**
  * 咱們但願傳遞一個參數 y 到該方法,在方法裏判斷Seq中是否包含該參數.
  * 但這樣寫的實際結果是 y 能夠匹配任意類型,只不過是把 x 的值賦給y
  * @param y
  */
def check(y: Int): Unit = {
  for {x <- Seq(99, 100, 101)} {
    val str = x match {
      case y => "find y"
      case i: Int => "int:" + i
    }

    println(str)
  }
}

check(100)

def check0(y: Int): Unit = {
  for {x <- Seq(99, 100, 101)} {
    val str = x match {
      case `y` => "find y"
      case i: Int => "int:" + i
    }

    println(str)
  }
}

check0(100)


模式匹配之Sequences

先看一下Seq 的簡單操做,spa

scala> val seq1 = Seq(1,2,3)
seq1: Seq[Int] = List(1, 2, 3)

scala> val seq2 = Seq(4,5,6)
seq2: Seq[Int] = List(4, 5, 6)

scala> seq1 ++ seq2
res22: Seq[Int] = List(1, 2, 3, 4, 5, 6)

scala> seq1.head +: seq1.tail
res23: Seq[Int] = List(1, 2, 3)

scala> seq1 +: seq2
res24: Seq[Any] = List(List(1, 2, 3), 4, 5, 6)

scala> seq1 :+ seq2
res25: Seq[Any] = List(1, 2, 3, List(4, 5, 6))

seq1.head +: seq1.tail 就表示是一個Seq 類型的數據scala

以下的 Seq的模式匹配,code

/**
  * Created by xinxingegeya on 16/1/15.
  */
val nonEmptySeq = Seq(1, 2, 3, 4, 5)

val emptySeq = Seq.empty[Int]

val nonEmptyList = List(1, 2, 3, 4, 5)

val emptyList = Nil

val nonEmptyVector = Vector(1, 2, 3, 4, 5)

val emptyVector = Vector.empty[Int]

val nonEmptyMap = Map("one" -> 1, "two" -> 2, "three" -> 3)

val emptyMap = Map.empty[String, Int]

def seqToString[T](seq: Seq[T]): String = seq match {
  case head +: tail => s"$head +: " + seqToString(tail)
  case Nil => "Nil"
}

for (seq <- Seq(
  nonEmptySeq, emptySeq, nonEmptyList, emptyList,
  nonEmptyVector, emptyVector, nonEmptyMap.toSeq, emptyMap.toSeq)) {
  println(seqToString(seq))
}

輸出結果,three

1 +: 2 +: 3 +: 4 +: 5 +: Nilip

Nilci

1 +: 2 +: 3 +: 4 +: 5 +: Nilstring

Nilit

1 +: 2 +: 3 +: 4 +: 5 +: Nilast

Nil

(one,1) +: (two,2) +: (three,3) +: Nil

Nil

對於List 類型有以下的操做符,

scala> val l1 = List(1,2,3)
l1: List[Int] = List(1, 2, 3)

scala> val l2 = List(4,5,6)
l2: List[Int] = List(4, 5, 6)

scala> l1 :: l2
res25: List[Any] = List(List(1, 2, 3), 4, 5, 6)

scala> l1 ::: l2
res26: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> l1 ++ l2
res27: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> val l3 = l1 :: l2
l3: List[Any] = List(List(1, 2, 3), 4, 5, 6)

scala> l3.head
res29: Any = List(1, 2, 3)

scala> l1 :: l2

res25: List[Any] = List(List(1, 2, 3), 4, 5, 6)

這種表示把 l1 放在 l2 的head部分。


模式匹配之Tuples

val langs = Seq(
  ("Scala", "Martin", "Odersky"),
  ("Clojure", "Rich", "Hickey"),
  ("Lisp", "John", "McCarthy")
)

for (tuple <- langs) {
  tuple match {
    case ("Scala", _, _) => println("Found Scala")
    case (lang, first, last) => println(s"Found other language : $lang ($first,$last)")
  }
}

使用 zipWithIndex 函數把元組和下標合併成一個pair,經過match遍歷。

val itemsCosts = Seq(("Pencil", 12), ("Paper", 23), ("Notebook", 223))

val itemCostsIndex = itemsCosts.zipWithIndex

for (i <- itemCostsIndex) {
  i match {
    case ((item, cost), index) => println(s"$index:$item costs $cost")
  }
}

=========END=========

相關文章
相關標籤/搜索