Black monkey house: Scala extractor

Pattern matching, matching what constitutes it? That is, case in unapply method returns a collection of some match was successful, the collection was returned none match fails. Let's look at a few examples discussed in detail do.

The so-called extractor, is an object with unapply method. Unapply direction we can apply a method as the associated method of operating a subject, the method accepts configuration parameters apply, and then turn them into an object, the method takes a unapply object and extract values, these values ​​are usually used to construct the object is the original the value

1、unapply

apply
- the Map (> 10 "Alex")
unapply
Map ( "Alex")
was deblocked using unapply encapsulated using apply data, data
- unapply calls, incoming Number
- Return value and receives the return value is None, or s Some
- If s Some, which will unlock and assign a value to which n (is the case Square (n) in n)

//创建object Square:
object Square {
  def unapply(arg: Double): Option[Double] = Some(math.sqrt(arg))
}
//模式匹配使用
val number: Double = 36.0
number match {
 //case 匹配过程中  默认调用unapply 自动调用</pre>
 case Square(n) => println(s"square ro ot of $number is $n")
 case _ => println("nothing matched")
}

2、unapplySeq

To extract a value of the sequence of any length, we should unapplySeq named our method. It returns an Option [Seq [A]], where A is the type of value is extracted, for example, Name extractor may sequence all components of the output of the name

- call unapplySeq, passing namesString
- receiving a return value and the return value is None, s Some or
- if Some, then uncouples
- if the number of elements of the sequence obtained after the judgment is untied three
- If three, put the three elements taken separately, assigned to the first, second and third

//创建object Names
object Names {
  def unapplySeq(str: String): Option[Seq[String]] = {
    if (str.trim.contains(",")){
      Some(str.split(","))
    }else{
      None
    }
  }
}
    
    
    //模式匹配使用
    val str: String = "Alex,Bob,Kotlin"
    str match {
      case Names(x, y, z) => println(x + "," + y + "," + z)
    }


    val namesString = "Alice,Bob,Thomas"
    namesString match {
      case Names(first, second, third) => {
        println("the string contains three people's names")
        println(s"$first $second $third")
      }
      case Names(x, y, z, d) => println(x + "-" + y + "-" + z + "-" + d)
      case Names(x, y, _*) => println(x + "-" + y)
      case _ => println("nothing matched")
    }

(1) call unapplySeq, passing namesString
receive a return value (2) and the return value is None, or s Some
(. 3) if Some, then unwound
(4) obtained after a sequence of elements in the unlock determination whether the number is three
(5) If it is three, three elements are put out, assigned to the first, second and third
if not unapplySeq method and pattern match this combination between grammar, writing our own code to do this five things will become very complicated.
尖叫提示:不要同时提供相同入参类型的unapply和unapplySeq方法

Reproduced in: https: //www.jianshu.com/p/28f6b04bf78e

Guess you like

Origin blog.csdn.net/weixin_34204057/article/details/91182455