scala 构造

Scala Currying is the process of transforming a function that takes multiple arguments into a single argument.

Scala Currying是将包含多个参数的函数转换为单个参数的过程。

Consider an example of multiplying two numbers .Open the scala REPL shell and create the multiply method as

考虑一个将两个数字相乘的示例。打开scala REPL shell并创建乘法方法为

def multiply(a:Int,b:Int) = a*b
Output:multiply: (a: Int, b: Int)Int

The multiply function accepts two arguments a and b of Integer data type and return the result of a*b which is of Integer data type.

乘法函数接受Integer数据类型的两个参数a和b并返回a * b的结果,该结果是Integer数据类型。

Invoke the method as multiply(4,5) and you will get output as res10:Int = 20

将方法调用为乘法(4,5),您将获得输出为res10:Int = 20

Now let us apply the currying for this function as;

现在让我们将此函数应用为currying;

def multiply(a:Int) = (b:Int) => a*b

Output: multiply: (a: Int)Int => Int

输出:乘以:(a:Int)Int => Int

The multiply function takes a single argument a of Integer data type in the function declaration. Inside the function it takes one more parameter b of Integer data type and return a* b as the result.

乘法函数在函数声明中采用Integer数据类型的单个参数a。 在函数内部,它需要再加上一个Integer数据类型的参数b并返回a * b作为结果。

Suppose if we invoke the function with single argument the function closure reference is returned as

假设如果我们使用单个参数调用函数,则函数闭包引用返回为

multiply(5)
res10: Int => Int = <function1>

Invoke the function by passing two arguments as

通过将两个参数传递为来调用该函数

multiply(4)(5)
res11:Int = 20

自动类型相关的封闭构造 (Automatic Type Dependent Closure Construction)

In scala, parameterless function names allows as parameters of methods. When such methods are called the nullary functions are evaluated and not the actual parameters for the parameterless function names. The nullary function encapsulates computation of the corresponding parameter called call-by-name evaluation.

在scala中,无参数函数名称允许作为方法的参数。 调用此类方法时,将评估无效函数,而不评估无参数函数名称的实际参数。 无效函数封装了称为“按名字求值”评估的相应参数的计算。

Let’s look at an example to understand this more clearly.

让我们看一个例子,以更清楚地理解这一点。

TypeDependent.scala

TypeDependent.scala

package com.journaldev.scalaobject TypeDependent {def main(args: Array[String]) {def forloop(rule: => Boolean)(body: => Unit): Unit =if (rule) {bodyforloop(rule)(body)}var i = 7forloop(i > 2) {println("i: " + i)i -= 1}}
}

We are creating an object TypeDependent which defines a method “forloop”. This method takes two parameters rule and body. Whenever the formal parameters are used in the body of forloop, the implicitly created nullary functions will be evaluated. Here we are implementing for loop, below image shows the output of above program.

我们正在创建一个对象TypeDependent ,该对象定义了一种方法“ forloop”。 该方法采用规则和正文两个参数。 每当在forloop主体中使用形式参数时,都会评估隐式创建的null函数。 这里我们实现了for循环,下图显示了上面程序的输出。

Now let us have a look at more complex example with more operations.

现在让我们看一下具有更多操作的更复杂的示例。

Typedep.scala

Typedep.scala

package com.journaldev.scalaobject Typedep extends App {def t1(body: => Unit): Criteria =new Criteria(body)protected class Criteria(body: => Unit) {def condition(rule: => Boolean) {bodyif (!rule) condition(rule)}}var x = 7t1 {println("x: " + x)x -= 1} condition (x == 2)
}

We are creating a scala object which contains a method t1 that accepts body parameter of Unit data type and returns an instance of class Criteria. The criteria class defines has a method “condition” which checks the condition if x == 2 after decrementing the value of x. Below image shows the output of the above program.

我们正在创建一个scala对象,其中包含方法t1,该方法接受Unit数据类型的body参数并返回Criteria类的实例。 标准类定义了一个方法“ condition”,该方法在递减x的值后,如果x == 2,则检查条件。 下图显示了以上程序的输出。

Personally I don’t like this feature a lot because of complexity, but it’s good to know.

就个人而言,由于复杂性,我不太喜欢此功能,但是很高兴知道。

翻译自: https://www.journaldev.com/8534/scala-currying-and-automatic-type-dependent-closure-construction

scala 构造

scala 构造_Scala咖喱和自动类型依赖的封闭构造相关推荐

  1. Scala——(常用类型与字面量,Scala类层次结构,值与变量自动类型推断,操作符,块表达式和赋值语句,输出和输出,字符串插值器,对象相等性)

    文章目录 常用类型与字面量 Scala类层次结构 值与变量&自动类型推断 操作符 块表达式和赋值语句 输出和输出 字符串插值器 对象相等性 常用类型与字面量 Scala和Java一样,有8种数 ...

  2. Eclipse下新建Maven项目、自动打依赖jar包

    当我们无法从本地仓库找到需要的构件的时候,就会从远程仓库下载构件至本地仓库.一般地,对于每个人来说,书房只有一个,但外面的书店有很多,类似第,对于Maven来说,每个用户只有一个本地仓库,但可以配置访 ...

  3. spring自动装配依赖包_解决Spring自动装配中的循环依赖

    spring自动装配依赖包 我认为这篇文章是在企业应用程序开发中使用Spring的最佳实践. 使用Spring编写企业Web应用程序时,服务层中的服务量可能会增加. 服务层中的每个服务可能会消耗其他服 ...

  4. make--变量与函数的综合示例 自动生成依赖关系

    一.变量与函数的示例 示例的要求 1.自动生成target文件夹存放可执行文件 2.自动生成objs文件夹存放编译生成的目标文件 3.支持调试版本的编译选项 4.考虑代码的扩展性 完成该示例所需的 1 ...

  5. IDEA maven项目查自动查看依赖关系,解决包冲突问题

    IDEA maven项目查自动查看依赖关系,解决包冲突问题 参考文章: (1)IDEA maven项目查自动查看依赖关系,解决包冲突问题 (2)https://www.cnblogs.com/jpfs ...

  6. 【Groovy】Groovy 动态语言特性 ( Groovy 中函数实参自动类型推断 | 函数动态参数注意事项 )

    文章目录 前言 一.Groovy 中函数实参自动类型推断 二.函数动态参数注意事项 三.完整代码示例 前言 Groovy 是动态语言 , Java 是静态语言 ; 本篇博客讨论 Groovy 中 , ...

  7. 【Groovy】Groovy 动态语言特性 ( Groovy 中的变量自动类型推断以及动态调用 | Java 中必须为变量指定其类型 )

    文章目录 前言 一.Groovy 动态语言 二.Groovy 中的变量自动类型推断及动态调用 三.Java 中必须为变量指定其类型 前言 Groovy 是动态语言 , Java 是静态语言 ; 一.G ...

  8. auto自动类型推断

    文章目录 1 auto 1 auto auto在C++11之前是自动变量的含义(栈变量,局部变量),在C++11后赋予其新的作用:自动类型推断. auto可以在声明变量的时候根据初始值的类型自动为此变 ...

  9. 简自动类型提升,精度损失类型强制转换,常用转义字符,简单帮你回顾Java基本数据类型整形浮点型字符型布尔型Boolean及其运算规则

    文章目录 整形 浮点型 字符型 布尔型boolean 自动类型提升 强制类型转换 注意 整形 bit是计算机中的最小存储单位. byte是计算机中的基本存储单元. 1MB=1024KB--1KB=10 ...

最新文章

  1. 不同表结构数据迁移_数据结构:哈希 哈希函数 哈希表
  2. 无线网卡实现AP 热点功能,共享Internet连接设置
  3. 【C 语言】结构体 ( 结构体类型变量初始化 | 定义变量时进行初始化 | 定义隐式结构体时声明变量并初始化 | 定义普通结构体时声明变量并初始化 )
  4. java 数组json_如何在Java中创建JSON数组
  5. 2017-2018-1 20162306 实验五实验报告
  6. 在线实时大数据平台Storm并行和通信机制理解
  7. Android自定义View实现滴滴验证码输入框效果
  8. VS也可以这样进行快捷安装
  9. centos7创建asm磁盘_ASM环境下防止误将数据文件扩容到本地文件系统的方法
  10. 大数据量的存储分表常见算法
  11. 携号转网时间明确了: 下半年在全国实施!
  12. 变量不在选择列表中_Python3中的表达式运算符
  13. Spoj REPEATS 后缀自动机+set
  14. paip.yxshopV4.7.1的安装不能用的问题
  15. Home Assistant系列 -- 设置界面语言与地理位置
  16. html输入框素材,html input 标签
  17. 两阶段目标检测原理详解--SPPNet
  18. Android开发——查询/杀死手机里正在运行的进程
  19. js之浏览器本地存储webstorage
  20. u盘启动 联想一体机_联想一体机怎么设置U盘启动?

热门文章

  1. python 将一个列表切割成随机指定长度的多个列表
  2. [转载] kotlin 字符串_Kotlin基本类型字符串
  3. [转载] python接口测试:封装get和post请求+重新封装requests类
  4. [转载] 【python第四天】 注释和缩进
  5. FPGA基础知识之主要的FPGA生产厂商介绍
  6. 关于SpringBoot和Thymeleaf模板中遇到的问题
  7. Hamilton四元数
  8. Apache Hadoop 源码阅读(陆续更新)
  9. Flume-NG源码阅读之Interceptor(原创)
  10. 学习的最大动力是想要更加减少重复性工作