码迷,mamicode.com
首页 > 其他好文 > 详细

大数据笔记(二十五)——Scala函数式编程

时间:2018-03-30 23:07:05      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:print   img   log   list   map   示例   hello   例子   注意   


===================== Scala函数式编程 ========================

一、Scala中的函数
(*) 函数是Scala中的头等公民,就和数字一样,可以在变量中存放函数,即:将函数作为变量的值(值函数)

def myFunc1(name:String):String = "Hello " + name
println(myFunc1("Tom"))

def myFunc2():String = "Hello World"

//值函数:把函数作为变量的值
val v1 = myFunc1("Tom")
val v2 = myFunc2()

//再将v1付给myFunc1
println(myFunc1(v1))

 

运行:

myFunc1: myFunc1[](val name: String) => String
Hello Tom
res0: Unit = ()

myFunc2: myFunc2[]() => String

v1: String = Hello Tom
v2: String = Hello World


Hello Hello Tom
res1: Unit = ()

 

二、匿名函数:没有名字的函数

//匿名函数:没有名字的函数
// 完整: def myFunc3(x:Int) = x * 3
(x:Int) => x*3

//举例:Array(1,2,3) ====> (3,6,9)
Array(1,2,3).map((x:Int) => x*3)

 

运行:

res2: Int => Int = <function1>
res3: Array[Int] = Array(3, 6, 9)

 

三、高阶函数:带函数参数的函数
注意:把一个函数作为另外一个函数的参数值

四、高阶函数示例

//高阶函数
import scala.math._

//对数字10进行某种运算
//f : 就是执行的运算
def someAction(f:(Double)=>Double) = f(10)

//测试
//情况1:开平方
someAction(sqrt)
someAction(sin)

//另一个例子
def mytest(x:Int,y:Int):Int = { x*y + 10}

//定义一个高阶函数
def myFunction(f:(Int,Int)=>Int,x:Int,y:Int)=f(x,y)

//调用
myFunction(mytest,2,3)

 

技术分享图片

 

运行:

import scala.math._


someAction: someAction[](val f: Double => Double) => Double

res0: Double = 3.1622776601683795
res1: Double = -0.5440211108893698

mytest: mytest[](val x: Int,val y: Int) => Int

myFunction: myFunction[](val f: (Int, Int) => Int,val x: Int,val y: Int) => Int

res2: Int = 16

 

数据: 

val numbers = List(1,2,3,4,5,6,7,8,9,10)

 

map: 作用于列表中的每个元素,并且返回一个新的列表

numbers.map((i:Int) => i*2)

foreach: 跟map一样,没有返回值

numbers.foreach((i:Int) => i*2)


filter: 移除函数函数false的元素
返回所有能够被2整除的元素

numbers.filter((i:Int)=> i%2 ==0 )

zip: 把两个列表合并到一个列表中

List(1,2,3).zip(List(4,5,6))

数据: 

val numbers = List(1,2,3,4,5,6,7,8,9,10)

partition: 能被2整除的放到一个分区中,不能被整除的放到另一个分区中

numbers.partition((i:Int) => i%2 ==0)

find: 第一个匹配条件的元素
找到第一个能被3整除的元素

numbers.find((x:Int) => x%3 == 0)

numbers.find(_ % 3 == 0)


flatten: 把一个嵌套的结构展开

List(List(1,2,3),List(4,5,6)).flatten


flatMap: 压平,结合了map和flatten的功能

var myList = List(List(1,2,3),List(4,5,6))
myList.flatMap(x=>x.map(_ *2))

分为两步
1、List(1,2,3),List(4,5,6) ===> 合并成一个List
2、再乘以2

运行以上例子的结果:

numbers: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

res0: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

res1: Unit = ()

res2: List[Int] = List(2, 4, 6, 8, 10)

res3: List[(Int, Int)] = List((1,4), (2,5), (3,6))

res4: (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9))
res5: Option[Int] = Some(3)

res6: Option[Int] = Some(3)

res7: List[Int] = List(1, 2, 3, 4, 5, 6)


myList: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))
res8: List[Int] = List(2, 4, 6, 8, 10, 12)
score: scala.collection.mutable.Map[String,Int] = Map(Mike -> 90)
res9: Unit = ()
Map(Mike -> 85)
res10: Unit = ()

 


五、闭包


六、柯里化:Currying

 

大数据笔记(二十五)——Scala函数式编程

标签:print   img   log   list   map   示例   hello   例子   注意   

原文地址:https://www.cnblogs.com/lingluo2017/p/8678750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!