scala进阶24-提取器与模式匹配

来源:互联网 发布:php扩展安装 编辑:程序博客网 时间:2024/05/17 08:56
/**  * 定义析构器(解构器)  * 析构的时候unapply回被调用  * 传入的是要匹配的对象,返回的是要匹配出来的具体内容(解构后的元素)  */object :> {  def unapply[A](list: List[A]) = {    Some((list.init, list.last))//init是最后一个元素之前的元素  }}object Extractor_Advanced {  def main(args: Array[String]): Unit = {    //匹配: 前面的元素任意,last是9    (1 to 9).toList match { case _ :> 9 => println("Hadoop") }    (1 to 9).toList match { case _ :> 8 :> 9 => println("Spark")} //右结合    (1 to 9).toList match { case :>(:>(_, 8), 9) => println("Flink")}  }}

0 0
原创粉丝点击