scala 函数中嵌套函数_如何在Scala中将函数转换为部分函数?
scala 函数中嵌套函数
First, let's see what is a function and a partial function, and then we will see their conversion process.
首先,让我们看看什么是函数和部分函数,然后看它们的转换过程。
Function in Scala is a block of code that is used to perform a specific task. Using a function you can name this block of code and reuse it when you need it.
Scala中的Function是用于执行特定任务的代码块。 您可以使用函数来命名此代码块,并在需要时重新使用它。
Example:
例:
def divide100(a : Int) : Int = {return 100/a;
}
Partial function in Scala are functions that return values only for a specific set of values i.e. this function is not able to return values for some input values.
Scala中的部分函数是仅返回特定值集的值的函数,即该函数无法返回某些输入值的值。
Example:
例:
def divide100 = new PartialFunction[Int , Int] {def isDefinedAt(a : Int) = a!=0
def apply(a: Int) = 100/a
}
将函数转换为部分函数 (Convert function to partial function)
You can replace a function with a partial function as the definition of the function cannot be changed by we will call the given function using our partial function which will convert the function to function.
您可以将函数替换为部分函数,因为函数的定义无法更改,因为我们将使用部分函数调用给定函数,这会将函数转换为函数。
Scala程序将功能转换为部分功能 (Scala Program to convert function to partial function)
//Convert function to partial function
object MyClass {def divide100(x:Int) = 100/x;
def dividePar = new PartialFunction[Int , Int] {def isDefinedAt(a : Int) = a!=0
def apply(a: Int) = divide100(a)
}
def main(args: Array[String]) {print("100 divided by the number is " + dividePar(20));
}
}
Output:
输出:
100 divided by the number is 5
翻译自: https://www.includehelp.com/scala/convert-function-to-partial-function-in-scala.aspx
scala 函数中嵌套函数
scala 函数中嵌套函数_如何在Scala中将函数转换为部分函数?相关推荐
- java 方法当参数_如何在Java中将函数作为参数传递?
如何在Java中将函数作为参数传递? 这个问题在这里已有答案: Java Pass方法作为参数 14个答案 是否可以将方法作为参 ...
- java中long如何使用_如何在Java中将long转换为int?
问题 如何在Java中将long转换为int? #1 热门回答(218 赞) 简单类型转换应该这样做: long l = 100000; int i = (int) l; 但请注意,大数(通常大于21 ...
- python中list函数中variables变量_如何在Python中使用变量,浅谈,Pytorch,的,Variable,方法...
Variable的基本概念 autograd.Variable 是包的核心类.它包装了张量,并且支持几乎所有的操作.一旦你完成了你的计算, 就可以调用 .backward() 方法 来 自动计算所有的 ...
- java matlab 矩阵_如何在MATLAB中将函数应用于矩阵的每一行/列?
你可能想要更模糊的Matlab函数bsxfun . 从Matlab文档中,bsxfun "applies the element-by-element binary operation sp ...
- scala中字符串计数_如何在Scala中创建一系列字符?
scala中字符串计数 The range is a set of data from a lower value to a larger value. In Scala, we have an ea ...
- java 函数 作为参数_如何在Java中将函数作为参数传递?
小编典典 Java 8及以上 如果你的类或接口只有一个抽象方法(有时称为SAM type),则使用Java 8+ lambda表达式,例如: public interface MyInterface ...
- python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入
在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...
- java中如何将字符串转化为字符_如何在Java中将字符串转换为运算符?
参见英文答案 > Is it possible to pass arithmetic operators to a method in java? ...
- python中定义无符号整数_如何在python中将有符号整数转换为无符号整数
假设: >你有2'补码表示;和, > By(unsigned long)表示无符号32位整数, 那么你只需要向负值添加2 ** 32(或1 <<32). 例如,将此值应用于-1 ...
- python日期转换为整数_如何在python中将datetime转换为整数
这取决于整数应该编码什么.您可以将日期从以前的某个时间转换为毫秒数.人们通常在1970年1月1日或1900年1月1日上午12:00时开始测量,并以整数毫秒为单位测量时间.datetime模块(或其他类 ...
最新文章
- linux动态库命名规则
- java drools5_Java Drools5.1 规则流基础【示例】
- oracle 用mybatis生成主键
- springboot集成swagger2测试接口
- Windows平台Eclipse配置Maven
- python面向对象编程类的成员总结
- Mockito框架代码学习调试
- C++ 11 nullptr关键字
- 使用Spark Streaming SQL基于时间窗口进行数据统计
- 西塘游(2007-08-14)
- 下载频道用户使用指南!!
- 冰河木马学习之监听服务端失败
- 基于STM32F405平台的多摩川协议编码器通讯过程(2)
- golang对接paypal支付
- Linux中实现定时任务详解
- 人工智能导论——遗传算法求解TSP问题实验
- Java数据库篇之mysql VS postgresql
- 前后端交互,密码加密,RSA 实现前端 js 加密,后端 go 解密
- 真实的任正非:告诉你一个真实的华为
- Java获取当前年月日
热门文章
- 三态输出门实验报告注意事项_数电基础知识:各种IO输出的类型
- JDK源码解析之 java.lang.Throwable
- 12-容器之间link
- 10-Docker 网络
- Simple Addition expression【打表+二分】
- 【Spark】开发Spark选择Java还是Scala?
- Reactjs 踏坑指南1: 一些概念
- 第二个冲刺期的第六天
- Launch Instruments
- 《jQuery、jQuery UI及jQuery Mobile技巧与示例》——7.4 示例:使用按钮集装饰单选框...