大数据系列修炼-Scala课程07

由于昨天下班后有点困,就没有来及写博客,今天会把它补上!把这个习惯坚持下去!

关于Scala高阶函数详解

  1.Scala高阶函数代码实现:高阶函数就是在我们函数中套用函数

  2.高阶函数代码详解:高阶函数能够让方法的调用更加便捷

  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  //(1 to 9)数组中的map方法向数组中放* 用foreach用于来循环 println _ 表示函数,把函数当做参数进行传递
  (1 to 9).map("*" * _).foreach(println _)        //> *
                                                  //| **
                                                  //| ***
                                                  //| ****
                                                  //| *****
                                                  //| ******
                                                  //| *******
                                                  //| ********
                                                  //| *********
  //过滤后被2整除的遍历  println也当做参数进行传递这就是高阶函数
  (1 to 9).filter(_ % 2 == 0) .foreach(println)   //> 2
                                                  //| 4
                                                  //| 6
                                                  //| 8
  //_*_代表数组中两个数据相乘 也属于高阶函数
  println((1 to 9).reduceLeft(_ * _))             //> 362880
  //把字符串用空格分割在按照长度排序
   "Spark is the most exciting thing happening in big data today".split(" ").
        sortWith(_.length < _.length).foreach(println)
                                                  //> is
                                                  //| in
                                                  //| the
                                                  //| big
                                                  //| most
                                                  //| data
                                                  //| Spark
                                                  //| thing
                                                  //| today
                                                  //| exciting
                                                  //| happening
   //ceil函数表示向上取值
   val fun = ceil _                               //> fun  : Double => Double = <function1>
   val num = 3.14                                 //> num  : Double = 3.14
   fun(num)                                       //> res0: Double = 4.0
   Array(3.14, 1.42, 2.0).map(fun)//fun函数当做函数传递   //> res1: Array[Double] = Array(4.0, 2.0, 2.0)
   //函数表示三的倍数
    val triple = (x: Double) => 3 * x             //> triple  : Double => Double = <function1>
    Array(3.14, 1.42, 2.0).map((x: Double) => 3 * x)
                                                  //> res2: Array[Double] = Array(9.42, 4.26, 6.0)
    Array(3.14, 1.42, 2.0).map{ (x: Double) => 3 * x }
                                                  //> res3: Array[Double] = Array(9.42, 4.26, 6.0)
    //定义高阶函数
    def high_order_functions(f: (Double) => Double) = f(0.25)
                                                  //> high_order_functions: (f: Double => Double)Double
    println(high_order_functions(ceil _))         //> 1.0
    println(high_order_functions(sqrt _))         //> 0.5
    //函数相乘
    def mulBy(factor: Double) = (x: Double) => factor * x
                                                  //> mulBy: (factor: Double)Double => Double
    val quintuple = mulBy(5)                      //> quintuple  : Double => Double = <function1>
    println(quintuple(20))                        //> 100.0
         //3的倍数解析
    println(high_order_functions((x: Double) => 3 * x))
                                                  //> 0.75
    high_order_functions((x) => 3 * x)            //> res4: Double = 0.75
    high_order_functions(x => 3 * x)              //> res5: Double = 0.75
    
    println(high_order_functions(3 * _))          //> 0.75
    
    val fun2 = 3 * (_: Double)                    //> fun2  : Double => Double = <function1>
    val fun3: (Double) => Double = 3 * _          //> fun3  : Double => Double = <function1>

Scala中SAM转换讲解

  1.SAM的意义:隐式的转换

  2.SAM转换详解

  var data = 0
  val frame = new JFrame("SAM Testing");
  val jButton = new JButton("Counter")
  jButton.addActionListener(new ActionListener {
    override def actionPerformed(event: ActionEvent) {
      data += 1
      println(data)
    }
  })
  //隐式函数就把一些函数定义,好比java中的函数定义模块-->这就可以隐式转换
  implicit def convertedAction(action: (ActionEvent) => Unit) = 
    new ActionListener {
      override def actionPerformed(event: ActionEvent) { action(event) }
    }
  //这样是写代码的时候只关心业务逻辑,没有必要去编写没有关系的代码,直接写所需的结果  
  jButton.addActionListener((event: ActionEvent) => {data += 1; println(data)})
    //这跟java中的gui编程一样  
    frame.setContentPane(jButton);   
    frame.pack();  
    frame.setVisible(true);  

Scala中curring讲解

  1.curring的定义:curring颗粒度,就是把参数简化成一个参数进行操作,返回后把另外进行计算

  2.curring在项目中重要性:在公式的推到中和计算中相当重要

    //curring颗粒度,就是把参数简化成一个参数进行操作,返回后把另外进行计算
    def multiple(x: Int, y: Int) = x * y
    def multipleOne(x: Int) = (y: Int) => x * y
    println(multipleOne(6)(7))
    //可以把以上的计算方式该项为下面的方法
    def curring(x: Int)(y: Int) = x * y
    
    println(curring(10)(10))

    val a = Array("Hello", "Spark")
    val b = Array("hello", "spark")
    //比较两个变量的是否相等
    println(a.corresponds(b)(_.equalsIgnoreCase(_)))

Scala中模式匹配入门讲解

  1.模式匹配分析:Scala中匹配模式比较灵活,可以传入参数和函数等

  2.在模式匹配中使用守卫:接下的blog中会深入讲解

  3.模式匹配中的变量使用

  //模式匹配在java中用switch来表示,这里面限制不是很多,int,byte,char,short
   val data =2
    data match {
      case 1 => println("First")
      case 2 => println("Second")
      case _ => println("Not Known Number")
    }
    //Scala中case中可以用条件函数也可以是函数,也可以传参数
    val result = data match {
    case i if i == 1 => "The First"
    case number if number ==2 => "The Second" + number
    case _ => "Not Known Number"
    }
    println(result)  
    "Spark !" foreach { c => println (
      c match {
        case ' ' => "space"
        case ch => "Char: " + ch
      }
      )}

这就是拖欠任务,今天会再写一篇关于Scala的学习!希望大家关注王家林老师的微信(18610086859),他会每天都更新大数据的视频!

最新大数据视频74讲:http://pan.baidu.com/s/1hqJByvU

本文百度云地址23-26讲:http://pan.baidu.com/s/1bOsSQ

原文地址:https://www.cnblogs.com/524646016-zhang/p/zhangsh_dt_scala_07.html