林子雨-2.4 函数式编程基础

目录

 

1、函数的定义与使用

定义

字面量

匿名函数

_

2、高阶函数

定义

3、针对容器的操作 (遍历、映射)

scala容器的标准遍历方法foreach

map

flatmap

4、过滤和规约

过滤

规约

reduce

fold

5、spark编程实例WordCount


1、函数的定义与使用

定义

函数==方法(见2.3)

字面量

函数也有函数字面量

函数式编程的实质:函数和整型一样,可以在不同的方法调用中操作,传递。

传入参数为value

函数值为counter

函数也有类型和值的概念。

但不是每个函数都需要给其命名(给函数值),因为用一次就不用了。

这时候就考虑使用匿名函数(Lambda表达式)

匿名函数

没有函数名称,可以直接放到方法的括号里面作为参数传入。

(参数) => 表达式

如果参数只有一个,圆括号可以省略

实例:

匿名函数可以赋值给函数类型的变量。

Int => Int 表示所声明的函数的类型

自动推断机制:不用写出函数的类型

_

一个下划线只能表示一个参数的一次出现

实例:

spark代码很喜欢使用_

2、高阶函数

定义

函数括号里的参数仍然是函数

3、针对容器的操作 (遍历、映射)

scala容器的标准遍历方法foreach

List:

Map:

中缀表示法

._1与._2:取元组或映射中的对象

x为map中的键值对映射;

case(k,v):(k,v)就是一个元组对象(结构)

tuple=(1,2,3) //自动认为tuple是元组结构

未简写

简写

map

对容器或集合中的元素进行一对一的映射

scala的容器或集合中都自带有map方法

map()括号中的语句为lambda函数

flatmap

4、过滤和规约

过滤

圈起来的部分为lambda表达式

规约

reduce

没有初始值

接受二元函数f为reduce的参数

规约实质

左规约与右规约:

fold

有初始值

5、spark编程实例WordCount

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课后作业 1. 简述zookeeper在HBase的作用 2. 如何获取链接zookeeper的客户端的信息 3. 简述如何用zookeeper实现一个普通的一对多fifo队列 4. 使用伪代码简述mapreduce的流程和间结果 其input和output都是fileinputformat和fileoutputformat 5. 编写程序实现倒排索引 首先准备数据:1.txt,文件内容如下: The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-availability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-available service on top of a cluster of computers, each of which may be prone to failures. 文件2.txt的内容如下所示: In order to scale the name service horizontally, federation uses multiple independent Namenodes/Namespaces. The Namenodes are federated, that is, the Namenodes are independent and don’t require coordination with each other. The datanodes are used as common storage for blocks by all the Namenodes. Each datanode registers with all the Namenodes in the cluster. Datanodes send periodic heartbeats and block reports and handles commands from the Namenodes. 建立类似的文件,放入/test1文件夹下 要求处理结果为: The 1.txt n 2.txt n Apache 1.txt n 2.txt n 其,n为某个单词在某个文件出现的次数,即,格式解释为:单词iterator<所在文件 出现次数>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值