Given that sequence, use reduceLeft to determine different properties about the collection. The following example shows how to get the sum of all the elements in the sequence:

scala> a.reduceLeft(_ + _)
res0: Int = 64

Don’t let the underscores throw you for a loop; they just stand for the two parameters that are passed into your function. You can write that code like this, if you prefer:

a.reduceLeft((x,y) => x + y)

The following examples show how to use reduceLeft to get the product of all elements in the sequence, the smallest value in the sequence, and the largest value:

scala> a.reduceLeft(_ * _)
res1: Int = 388800 scala> a.reduceLeft(_ min _)
res2: Int = 2 scala> a.reduceLeft(_ max _)
res3: Int = 20

scala _ parameter的更多相关文章

  1. Scala _ [underscore] magic

    I started learning Scala a few days before. Initially i was annoyed by the use of too many symbols i ...

  2. Scala _ 下划线

    1.引入包中的全部方法 import math._ //引入包中所有方法,与java中的*类似 2.表示集合元素 val a = (1 to 10).filter(_%2==0).map(_*2) / ...

  3. Scala underscore的用途

    _ 的用途 // import all import scala.io._ // import all, but hide Codec import scala.io.{Codec => _, ...

  4. Scala(一) —— 基础

    一.输出 println("Hello World") 二.变量与常量 1.变量用var表示,常量使用val表示 2.变量类型声明 var variableName : DateT ...

  5. Scala编程进阶

    跳出循环语句的3种方法... 2 多维数组... 3 Java数组与Scala数组缓冲的隐式转换... 3 Java Map与Scala Map的隐式转换... 3 Tuple拉链操作... 4 内部 ...

  6. Scala详解

    1       快速入门... 4 1.1             分号... 4 1.2             常变量声明... 4 1.2.1         val常量... 4 1.2.2  ...

  7. Scala编程 笔记

    date: 2019-08-07 11:15:00 updated: 2019-11-25 20:00:00 Scala编程 笔记 1. makeRDD 和 parallelize 生成 RDD de ...

  8. Scala基础语法 (一)

    如果你之前是一名 Java 程序员,并了解 Java 语言的基础知识,那么你能很快学会 Scala 的基础语法. Scala 与 Java 的最大区别是:Scala 语句末尾的分号 ; 是可选的. 我 ...

  9. Scala HandBook

    目录[-] 1.   Scala有多cool 1.1.     速度! 1.2.     易用的数据结构 1.3.     OOP+FP 1.4.     动态+静态 1.5.     DSL 1.6 ...

随机推荐

  1. Web应用程序与Web网站及部署在IIS中

    在Visual Studio可以创建 Web 应用程序项目或网站项目.通过选择 新建项目 或 打开项目 创建或打开一个 Web 应用程序项目在Visual Studio 文件 菜单. 通过选择 新建网 ...

  2. LeetCode 366. Find Leaves of Binary Tree

    原题链接在这里:https://leetcode.com/problems/find-leaves-of-binary-tree/#/description 题目: Given a binary tr ...

  3. Spring Aware接口---BeanNameAware BeanFactoryAware ApplicationContextAware

    前言 对于应用程序来说,应该尽量减少对spring api的耦合程度,然后有时候为了运用spring提供的一些功能,有必要让bean了解spring容器对其管理的细节信息,如让bean知道在容器中是以 ...

  4. fn project 生产环境使用

    此为官方的参考说明   Running Fn in Production The QuickStart guide is intended to quickly get started and kic ...

  5. fn project 对象模型

    Applications At the root of everything are applications. In fn, an application is essentially a grou ...

  6. iframe添加点击事件

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. bzoj 3277 & bzoj 3473,bzoj 2780 —— 广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

  8. Java面试题:栈和队列的实现

    面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min()的栈,要 ...

  9. 【Python学习笔记】在OSX下搭建opencv+python环境

    https://jjyap.wordpress.com/2014/05/24/installing-opencv-2-4-9-on-mac-osx-with-python-support/ 参照以上b ...

  10. Centos7手工安装Kubernetes集群

    安装Kubernetes集群有多种方式,前面介绍了Kubeadm的方式,本文将介绍手工安装的方法. 安装环境有3台Azure上的VM: Hkube01:10.0.1.4 Hkube02:10.0.1. ...