scala worksheet demo

object worksheet_lp {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  var x = 1                                       //> x  : Int = 1
  println(x)                                      //> 1
  //for
  var l = List("abc", "bc", "c")                  //> l  : List[String] = List(abc, bc, c)
  for (
    s <- l
  ) println(s)                                    //> abc
                                                  //| bc
                                                  //| c
  //filter
  for {
    s <- l
    if (s.length > 2)
  } println(s)                                    //> abc

  //yield
  for {
    s <- l
    s1 = s.toUpperCase()
  } yield (s1)                                    //> res0: List[String] = List(ABC, BC, C)

  //try catch finally
  var result = try { Integer.parseInt("do") } catch {
    case _ => 2
  } finally { println("finally") }                //> finally
                                                  //| result  : Int = 2
  //match
  var caseVar = 1                                 //> caseVar  : Int = 1
  var result_case = caseVar match {
    case 1 => "first"
    case 2 => "second"
    case _ => "others"
  }                                               //> result_case  : String = first
}

 

posted @ 2017-02-28 15:44  天天好心情啊  阅读(341)  评论(0编辑  收藏  举报