模式匹配基本语法与在集合中的使用
来源:互联网 发布:韩国女主播软件 编辑:程序博客网 时间:2024/06/05 00:08
场景
模式匹配基本语法与在集合中的使用实验
package com.scode.scala/** * author: Ivy Peng * function: 1、学习模式匹配 基本语法与在集合中的使用 * 2、了解提取器Extractor工作机制(待进一步理解) * date:2016/03/20 11.47 * sum: * 模式匹配语法:varName match * { * case matchContent => ops * ... * case * } * 1、可以把匹配的内容赋值给 matchContent,然后在 ops中对其进行操作 * 2、case分支不需要以 break结尾 */object MatchOps{ def main(args:Array[String])= { matchBase match_typeList(List(1,2)) match_tuple((0,3)) val pattern = "([0-9]+) ([a-z]+)".r "2016032 nice" match { case pattern(num,item)=> println(num+":"+item) } } def matchBase() { val data = 2 data match { case 1=> println("First") case 2=> println("Second") case _ => println("Unknow input") } //把匹配的内容,传递给 val i,在 => 后面可以使用 i val result = data match { case i if i==1 => "First" case num if num==2 => "Second" case _=> println("Unknow input") } println(result) "Spark".foreach { x => print( x match { case ' ' => "space" case ch => ch } ) } } def match_type(typ:Any)=typ match { case p:Int => println("Integer") case str:String => println("String") case m:Map[_,_] => m.foreach(println) case Array(0) => println("Array"+"0") case Array(x,y) => println("Array"+x+y) case Array(0,_*)=> println("Array"+"...") case _=> println("something else") } def match_typeList(t:Any)= t match { case 0::Nil => println("List:"+"0") case x::y::Nil=> println("List:"+x+y) case 0::tail=> println("List:"+"0 ...") case _=> println("Something else") } def match_tuple(tuple:Any)= tuple match { case (0,_)=> println("tuple:"+ "0") case (_,2)=> println(". 2") }}
参考文献
scala 深入浅出实战经典 . 王家林模式 0 0
- 模式匹配基本语法与在集合中的使用
- sphinx检索语法与匹配模式备忘
- SASS 基本语法与使用
- F#入门:基本语法,模式匹配及List
- struts配置中的路径与模式匹配
- JavaScript中的模式匹配与正则表达式
- struts配置中的路径与模式匹配
- Erlang中的模块与模式匹配
- MongoDB的基本操作与在Python中的使用
- 在vi中使用模式匹配
- iOS Block的基本语法与使用
- Scala模式匹配语法讲解
- @objc在swift语法中的使用场景
- Markdown的语法&在SublimeText中的使用
- Matlab中的基本语法
- java中的基本语法
- lua中的基本语法
- Struct 在iOS中的集合中使用
- appcompat_v7/res/values-v21/themes_base.xml No resource found that matches the given name: androidXX
- 打印非负整数阶乘的函数
- 求字符串长度
- 基于DES加密的TCP聊天程序
- react学习札记(三)--自己的日历控件
- 模式匹配基本语法与在集合中的使用
- [POJ 1201]Intervals[差分约束]
- linux 扫描端口
- 第4周项目4程序分析(1)
- 第三次上机上机实践项目-项目5-(1)
- LeetCode-78-Subsets(回溯法)-Medium
- Python常用内置函数
- vsftpd的虚拟账户配置
- 阿里巴巴2017届实习生笔试题反思