大数据之scala基本语法学习

package edu.snnu.test

object list2 {
    //把字符串转化成一个char类型的list
     "99 Red Balloons".toList                  //> res0: List[Char] = List(9, 9,  , R, e, d,  , B, a, l, l, o, o, n, s)
     
     "99 Red Balloons".toList.filter(x => Character.isDigit(x))
                                                  //> res1: List[Char] = List(9, 9)
                                                  
    "99 Red Balloons".toList.takeWhile(x => x != 'B')
                                                  //> res2: List[Char] = List(9, 9,  , R, e, d,  )
 
     val c = List("x", "y", "z")               //> c  : List[String] = List(x, y, z)
     c.map(x => x.toUpperCase)                 //> res3: List[String] = List(X, Y, Z)
     
     //上面的语句可以简写为下面形式,因为scala中有下划线作为通配符
     c.map(_.toUpperCase())                    //> res4: List[String] = List(X, Y, Z)
 
     val a = List(1, 2, 3, 4)                  //> a  : List[Int] = List(1, 2, 3, 4)
     a.filter(_%2 == 1)                        //> res5: List[Int] = List(1, 3)
     
     //对a这个list中的每个奇数元素加上10
    a.filter(_%2 == 1).map(_+10)              //> res6: List[Int] = List(11, 13)
     
     val q = List(a, List(4, 5, 6))            //> q  : List[List[Int]] = List(List(1, 2, 3, 4), List(4, 5, 6))
     q.map(x => x.filter(_%2 == 0))            //> res7: List[List[Int]] = List(List(2, 4), List(4, 6))
     q.map(_.filter(_%2 == 0))                 //> res8: List[List[Int]] = List(List(2, 4), List(4, 6))
     
     //把有两个集合的集合中,内层中满足条件的元素变到一个list中
     q.flatMap {_.filter(_%2 == 0)}            //> res9: List[Int] = List(2, 4, 4, 6)
     
     
    
}

 

柯里化案例:

(x: Int, y : Int) => x + y                      //> res0: (Int, Int) => Int = <function2>
  
  //def curriedAdd(a: Int)(b:Int)(c:Int)(d:Int) = a + b*c*d
  def curriedAdd(a:Int,b:Int)(c:Int)(d:Int) = a + b*c*d
                                                  //> curriedAdd: (a: Int, b: Int)(c: Int)(d: Int)Int
  //curriedAdd(2)(2)(2)(2)
    
    val addOne = curriedAdd(4,3)_             //> addOne  : Int => (Int => Int) = <function1>
    
    val c = addOne(2)(3)                      //> c  : Int = 22
    println(addOne)                           //> <function1>

 

Map使用方法

package edu.snnu.test

object mapDemo {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet

    val p = Map(1 -> "David", 9 -> "Elwood", "rain" -> "明明")
                                                  //> p  : scala.collection.immutable.Map[Any,String] = Map(1 -> David, 9 -> Elwoo
                                                  //| d, rain -> 鏄庢槑)
    //通过key取map中对应的val
    p(1)                                      //> res0: String = David
    p(9)                                      //> res1: String = Elwood
    p("rain")                                 //> res2: String = 鏄庢槑
    
    //contains方法用来判断某个key在不在map里面
    p.contains(1)                             //> res3: Boolean = true
    
    p.contains(2)                             //> res4: Boolean = false
    
    //调用map变量的keys方法将会返回一个存储这个map所有key的list
    p.keys                                    //> res5: Iterable[Any] = Set(1, 9, rain)
    //val叶同样
    p.values                                  //> res6: Iterable[String] = MapLike(David, Elwood, 鏄庢槑)
    
    //添加一个(key,value)到map中
    val pp = p+(8->"Archer")                  //> pp  : scala.collection.immutable.Map[Any,String] = Map(1 -> David, 9 -> Elwo
                                                  //| od, rain -> 鏄庢槑, 8 -> Archer)
    p                                         //> res7: scala.collection.immutable.Map[Any,String] = Map(1 -> David, 9 -> Elwo
                                                  //| od, rain -> 鏄庢槑)
    
    pp                                        //> res8: scala.collection.immutable.Map[Any,String] = Map(1 -> David, 9 -> Elwo
                                                  //| od, rain -> 鏄庢槑, 8 -> Archer)
                                                  
     //删除一个(key, value):map变量减去要删除的键值对的key即可
     p-1                                       //> res9: scala.collection.immutable.Map[Any,String] = Map(9 -> Elwood, rain -> 
                                                  //| 鏄庢槑)
     p ++ List(2 ->"Alice", 5->"Bob")          //> res10: scala.collection.immutable.Map[Any,String] = Map(5 -> Bob, 1 -> David
                                                  //| , 9 -> Elwood, rain -> 鏄庢槑, 2 -> Alice)
                                                  
  p -- List(1, 9, 2, "rain")                      //> res11: scala.collection.immutable.Map[Any,String] = Map()
  
  p                                               //> res12: scala.collection.immutable.Map[Any,String] = Map(1 -> David, 9 -> Elw
                                                  //| ood, rain -> 鏄庢槑)
    p ++ List(2 ->"Alice", 5->"Bob") -- List(9, 1, "rain")
                                                  //> res13: scala.collection.immutable.Map[Any,String] = Map(5 -> Bob, 2 -> Alice
                                                  //| )
  
}

快速排序小案例

package edu.snnu.test

object qSort {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  def qSort(a:List[Int]):List[Int] = {
      if(a.length < 2) a
      else
          qSort(a.filter(_<a.head)) ++
          a.filter(_ == a.head) ++
          qSort(a.filter(_>a.head))
  }                                               //> qSort: (a: List[Int])List[Int]
  
  val l = List(1, 2, 3, 4, 5, 9, 8, 7, 6)         //> l  : List[Int] = List(1, 2, 3, 4, 5, 9, 8, 7, 6)
  qSort(List(4, 3, 2, 6, 7, 8, 9))                //> res0: List[Int] = List(2, 3, 4, 6, 7, 8, 9)
  qSort(l)                                        //> res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
  
  
}

range的使用

package edu.snnu.test

object Range {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  1 to 10                                         //> res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7,
                                                  //|  8, 9, 10)
  1 to 10 by 2                                    //> res1: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
  

    (1 to 10).toList                          //> res2: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    1 until 10                                //> res3: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
    //也就是说to上届是一个闭区间 ,until上界是一个闭区间
    val s = 1     #::2 #:: 3 #::Stream.empty
                                                  //> s  : <error> = Stream(1, ?)
}

reduceLeft以及foldleft规约的使用

package edu.snnu.test

object reduceLeft {
    //reduceLeft是一种规约方法
  val a = List(1, 2, 3, 4)                        //> a  : List[Int] = List(1, 2, 3, 4)
     //map的返回值和原来的值的类型是一样的,flatMap会把两层的map转化为一层的map
     a.reduceLeft((x, y) => x + y)             //> res0: Int = 10
    //简写方式,用两个下划线来代替这两个参数
    a.reduce(_+_)                             //> res1: Int = 10
    
    //foldleft也是一种规约方法
    a.foldLeft(1)(_+_)                        //> res2: Int = 11
    a.foldLeft(2)(_*_)                        //> res3: Int = 48
}

懒加载

package edu.snnu.test


object StreamDemo {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  //懒加载,在用的时候求值,按需求值
  //val a = 1 #:: 2 #:: 3 #:: Stream.empty
  
  //val stream = (1 to 10000000).toStream()
  
}

List的使用

package edu.snnu.test

object test1 {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  
  val a = List(1, 2, 3, 4)                        //> a  : List[Int] = List(1, 2, 3, 4)
  
  //把0添加到刚才的list里面,两个冒号为连接操作符,连接操作符的左边是成员,右边是一个list
  val b = 0::a                                    //> b  : List[Int] = List(0, 1, 2, 3, 4)
  
  val c = "x"::"y"::"z"::Nil                      //> c  : List[String] = List(x, y, z)
  
  //生成一个list(z)
  "z"::Nil                                        //> res0: List[String] = List(z)
  
  //三个毛冒号的连接操作符用于连接两个list
  a:::c                                           //> res1: List[Any] = List(1, 2, 3, 4, x, y, z)
  
  //list的head方法返回当前list的第一个元素
  a.head                                          //> res2: Int = 1
  b.head                                          //> res3: Int = 0
  c.head                                          //> res4: String = x
  
  //list的tail方法返回当前list里除了第一个元素外的所有元素组成的list,这样的列表我们称为尾列表
  a                                               //> res5: List[Int] = List(1, 2, 3, 4)
  a.tail                                          //> res6: List[Int] = List(2, 3, 4)
  b                                               //> res7: List[Int] = List(0, 1, 2, 3, 4)
  b.tail                                          //> res8: List[Int] = List(1, 2, 3, 4)
  
  a.isEmpty                                       //> res9: Boolean = false
  
     Nil                                       //> res10: scala.collection.immutable.Nil.type = List()
     Nil.isEmpty                               //> res11: Boolean = true
     
     def walkthru(l:List[Int]) :String = {
         if(l.isEmpty) ""
         else {
             println(l.tail)
             l.head.toString+""+walkthru(l.tail)
             
         }
     }                                         //> walkthru: (l: List[Int])String
  
     walkthru(List(1,2,3,4,5))                 //> List(2, 3, 4, 5)
                                                  //| List(3, 4, 5)
                                                  //| List(4, 5)
                                                  //| List(5)
                                                  //| List()
                                                  //| res12: String = 12345
  
  //对list a进行过滤,如果里面的元素x满足x%2 == 1,那么保留下来,否则就删除
  a.filter(x => x%2 == 1)                         //> res13: List[Int] = List(1, 3)
}

tuple的使用

package edu.snnu.test

object tuple {
  
  1 -> 2                                          //> res0: (Int, Int) = (1,2)
  
  //定义一个tuple
  val t = (1, "Alice", "Math", 95,5)              //> t  : (Int, String, String, Int, Int) = (1,Alice,Math,95,5)
   
  //获得tuple t的第一个成员
  t._1                                            //> res1: Int = 1
     //获得tuple t的第二个成员
     t._2                                      //> res2: String = Alice
     //第三个
     t._3                                      //> res3: String = Math
     
     //函数的返回值一般都只有一个值,如果有多个值的话我们就可以把这多个值封装在一个tuple里,
     
     val a = List(1, 2, 3, 4)                  //> a  : List[Int] = List(1, 2, 3, 4)
     
     //(Int, Int, Int)代表返回值是一个三个int数据的list
     def sumSq(in: List[Int]):(Int,Int,Int) = {
         //下面t代表(0, 0, 0)这个元组,v代表这个list中的每一个值
         in.foldLeft((0,0,0))((t,v) => (t._1 + 1, t._2+v, t._3+v*v))
     }                                         //> sumSq: (in: List[Int])(Int, Int, Int)
     
     sumSq(a)                                  //> res4: (Int, Int, Int) = (4,10,30)
     
}

 

posted on 2016-08-24 21:47  张明明_1  阅读(1034)  评论(0编辑  收藏  举报

导航