spark快速开发之scala基础之2控制流程
Published on 2017-08-10 11:58 in 暂未分类 with 是奉壹呀

spark快速开发之scala基础之2控制流程

判断结构

大体与java相当。scala没有三元表达式。

    val num = if(1>0) 1 else 0 //相当于匿名函数
    println(num)
    
    var num2 = 0
    if(1>0) num2 = 1 else num2 = 0
    
    println(num2)

 

选择结构

match。与java的stiwch相当。但scala的match强大很多。

 

  1. 通配符匹配(Wildcard Pattern Matching )

  2. 常量匹配 (Constant Pattern Matching )

  3. 变量匹配(Variable Pattern Matching )

  4. 构造函数匹配(Constructor Pattern Matching )

  5. 集合类型匹配(Sequence Pattern Matching )

  6. 元祖类型匹配(Tuple Pattern Matching )

  7. 类型匹配(Typed Pattern Matching )

 

复制代码
        // constant patterns
        case 0 => "zero"
        case true => "true"
        case "hello" => "you said 'hello'"
        case Nil => "an empty List"
        // sequence patterns
        case List(0, _, _) => "a three-element list with 0 as the first element"
        case List(1, _*) => "a list beginning with 1, having any number of elements"
        case Vector(1, _*) => "a vector starting with 1, having any number of elements"
        // tuples
        case (a, b) => s"got $a and $b"
        case (a, b, c) => s"got $a, $b, and $c"
        // constructor patterns
        case Person(first, "Alexander") => s"found an Alexander, first name = $first"
        case Dog("Suka") => "found a dog named Suka"
        // typed patterns
        case s: String => s"you gave me this string: $s"
        case i: Int => s"thanks for the int: $i"
        case f: Float => s"thanks for the float: $f"
        case a: Array[Int] => s"an array of int: ${a.mkString(",")}"
        case as: Array[String] => s"an array of strings: ${as.mkString(",")}"
        case d: Dog => s"dog: ${d.name}"
        case list: List[_] => s"thanks for the List: $list"
        case m: Map[_, _] => m.toString
        // the default wildcard pattern
        case _ => "Unknown"
复制代码

 

循环结构

while 

do while

与java相同。

for 可以多重循环,循环过滤。返回值。

复制代码
    val list = List("3423")
    for(t <- list){
      println(t)
    }
    
    for(i <- 1 to 10){//包含10
      println(i)
    }
    
    for(i <- 1 until 10){//不包含10
      println(i)
    }
    println("===================")
    for(i <- 1 to 10;if i> 5){//添加过滤条件
      println(i)
    }
    println("===================")
    for(i <- 1 to 10;j <- 1 to 10){
      println(i +" " + j)
    }
    println("===================")
    
     for (i <- 1 to 5) yield i * 2 
    
    var result = for(t <- list) yield t //result = list
    var result2 = for(t <- list)  yield t + "10"
    result.foreach(println)
复制代码

 

异常控制

   try{
      
    }catch{
      case ex : NullPointerException => ex.printStackTrace()
      case _: Exception => ""
    }

 

break  continue

scala没有这两个关键字。但是scala提供了Breaks类来达到相同的效果。

复制代码
 def continue() {
    for (i <- 1 to 10) {
      Breaks.breakable({
        if (i == 5) {
          Breaks.break()
        }
        println(i)
      })
    }
    println("break")
  }
复制代码

执行结果:

1
2
3
4
6
7
8
9
10
break

复制代码
 def break() {
    Breaks.breakable({
      for (i <- 1 to 10) {
        if (i == 5) {
          Breaks.break()
        }
        println(i)
      }
    })
    println("break")
  }
复制代码

执行结果:

1
2
3
4
break

 

posted @   是奉壹呀  阅读(206)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示