Scala Reduce操作(简化归约)reduce和fold
1 package chapter07
2
3 object Test15_HighLevelFunction_Reduce {
4 def main(args: Array[String]): Unit = {
5 val list = List(1,2,3,4)
6
7 // 1. reduce
8 println(list.reduce( _ + _ ))
9 println(list.reduceLeft(_ + _))
10 println(list.reduceRight(_ + _))
11
12 println("===========================")
13
14 val list2 = List(3,4,5,8,10)
15 println(list2.reduce(_ - _)) // -24
16 println(list2.reduceLeft(_ - _)) //-24
17 println(list2.reduceRight(_ - _)) // 3 - (4 - (5 - (8 - 10))), 6
18
19 println("===========================")
20 // 2. fold
21 println(list.fold(10)(_ + _)) // 10 + 1 + 2 + 3 + 4
22 println(list.foldLeft(10)(_ - _)) // 10 - 1 - 2 - 3 - 4
23 println(list2.foldRight(11)(_ - _)) // 3 - (4 - (5 - (8 - (10 - 11)))), -5
24 }
25 }
Scala Reduce操作(简化归约)reduce和fold的更多相关文章
- [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用
reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...
- Java8中聚合操作collect、reduce方法详解
Stream的基本概念 Stream和集合的区别: Stream不会自己存储元素.元素储存在底层集合或者根据需要产生.Stream操作符不会改变源对象.相反,它会返回一个持有结果的新的Stream.3 ...
- Scala实战高手****第6课 :零基础实战Scala集合操作及Spark源码解析
本课内容1.Spark中Scala集合操作鉴赏2.Scala集合操作实战 --------------------------------------------------------------- ...
- 大数据入门到精通8-spark RDD 复合key 和复合value 的map reduce操作
一.做基础数据准备 这次使用fights得数据. scala> val flights= sc.textFile("/user/hdfs/data/Flights/flights.cs ...
- C#数组的Map、Filter、Reduce操作
在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...
- 深入理解groupByKey、reduceByKey区别——本质就是一个local machine的reduce操作
下面来看看groupByKey和reduceByKey的区别: val conf = new SparkConf().setAppName("GroupAndReduce").se ...
- Scala集合操作
大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka. ...
- [Javascript] Advanced Reduce: Composing Functions with Reduce
Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...
- Scala文件操作
Scala中的文件操作基本可以依赖于Java的实现,包括输入.输出流的使用. object FileOps {def main(args: Array[String]) { val file = So ...
- Scala学习笔记——简化代码、柯里化、继承、特质
1.简化代码 package com.scala.first import java.io.File import javax.management.Query /** * Created by co ...
随机推荐
- Spring Security实现JDBC用户登录认证
在搭建博客后端服务框架时,我采用邮件注册+Spring Security登录认证方式,结合mysql数据库,给大家展示下具体是怎么整合的. 本篇是基于上一篇:spring boot实现邮箱验证码注册 ...
- C# EnumWindows示例代码
代码开箱即用,唯一需要处理的就是要提供一个进程的pid. using System; using System.Collections.Generic; using System.Linq; usin ...
- centos7.5 hadoop NAT 静态IP网络环境搭建
1 设置 VMware 网络环境 1. 选择VMNet8 并将子网IP 修改为 192.168.10.0,保证集群ip都在这个网段下 2. 选择NAT 设置,配置NAT的网关为 192.168.10. ...
- Hi3516开发笔记(八):Hi3516虚拟机交叉开发环境搭建之配置QtCreator开发交叉编译环境
海思开发专栏 上一篇:<Hi3516开发笔记(七):Hi3516虚拟机交叉开发环境搭建之交叉编译Qt>下一篇:<Hi3516开发笔记(九):在QtCreator开发环境中引入海思sd ...
- git修改地址三种方法
1.修改命令 git remote set-url origin [NEW_URL] 2.先删后加 git remote rm origin git remote add origin [url]3. ...
- 【Azure Function App】解决Function App For Container 遇见ServiceUnavailable的异常
问题描述 在使用Terraform创建Function App 后,部署函数时候遇见 ServiceUnavailable (Bad Request -- Encountered an error ( ...
- 【Azure 环境】如何解决Principal 2330xxxxxxxxxxxxxxxxxxxx31efc5 does not exist in the directory xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx问题
问题描述 在使用 Key Vault 和 Azure CLI 管理存储帐户密钥的官方文档中,其中有一步是"向 Key Vault 授予对你的存储帐户的访问权限", 其中CLI命令中 ...
- 【Azure 应用服务】App Service For Linux 怎么安装Composer,怎么安装PHP扩展,怎么来修改站点根路径启动程序?
问题一:App Service 的默认启动路径为wwwroot,如何修改到到PHP代码运行目录呢? 如Laravel的启动目录为public/?那如何修改呢? App Service 的默认 PHP ...
- Docker的使用记录
开始 这是第一个尝试在Leanote上面编写文章,我觉得最重要的事情就是能够保证md文件是能够移植的,否则如果这个软件不靠谱的话,我还能把文章移动到别的地方去.所以先写一篇文章看看效果如何,方便不方便 ...
- vscode ctrl + d 删除当前行 更改快捷键
vscode 删除当前行 更改快捷键