scala 函数调用_在Scala中按名称调用函数

scala 函数调用

函数按名称调用 (Functions call by name )

By default, the method of parameter passing in a programming language is "call by value". In this, the parameter is passed to a function which makes a copy of them an operates on them. In Scala also, the call by name is the default parameter passing method.

默认情况下,以编程语言传递参数的方法是“按值调用” 。 在这种情况下,参数被传递给一个函数,该函数使它们的副本对它们进行操作。 同样在Scala中,按名称调用是默认的参数传递方法。

Call by name is used in Scala when the program needs to pass an expression or a block of code as a parameter to a function. The code block passed as call by name in the program will not get executed until it is called by the function.

当程序需要将表达式或代码块作为参数传递给函数时, 在Scala中使用按名称调用 。 在程序中按名称传递作为调用传递的代码块,直到函数调用该代码块后,才能执行。

Syntax:

句法:

    def functionName(parameter => Datatype){
//Function body... contains the call by name call to the code block
}

Explanation:

说明:

This syntax initializes a call by name function call. Here the function's argument passed is a function and the datatype is the return type of the function that is called. The function body executes the call by name call to the function that evaluates to provide the value. The call is initiated by using the parameter name as specified in the program.

语法通过名称函数call初始化调用 。 这里传递的函数参数是一个函数,数据类型是所调用函数的返回类型。 函数主体通过名称调用执行对要评估以提供值的函数的调用。 通过使用程序中指定的参数名称来启动该调用。

Example:

例:

object Demo {
def multiply(n : Int) = {
(14*5);
}
def multiplier( t: => Long ) = {
println("Code to multiply the value by 5")
println("14 * 5 = " + t)
}
def main(args: Array[String]) {
println("Code to show call by name")
multiplier(multiply(14))
}
}

Output

输出量

Code to show call by name
Code to multiply the value by 5
14 * 5 = 70

Code explanation:

代码说明:

The above code is to display the use of call by name. The code prints the number multiplied by 5. The number in the code is 14. That is passed to the call by name function at the time of function call form the main call. The in the multiplier function after the code it needs to execute the multiply method is initiated and value is evaluated there to be returned to the calling function which prints the value i.e. 70.

上面的代码是按名称显示呼叫使用 。 该代码将打印乘以5的数字。代码中的数字为14。在从主调用进行函数调用时,该函数将传递给按名称调用。 在乘法器函数中,需要执行乘法方法的代码启动后,在其中求值,然后返回到打印该值(即70)的调用函数。

翻译自: https://www.includehelp.com/scala/functions-call-by-name-in-scala.aspx

scala 函数调用

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380622.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

电脑PHP漏洞啥意思,PHP漏洞详解

几个重要的php.ini选项Register Globalsphp>4.2.0,php.ini的register_globals选项的默认值预设为Off,当register_globals的设定为On时,程序可以接收来自服务器的各种环境变量,包括表单提交的变量,而且由于PHP不必事先初始化变量的值,从而导致很大的安全隐患.例1://check_adm…

3年前的小程序:破解需要delphi IDE 环境的vcl 控件

基本原理:有些vcl组件未注册的话,会显示没有注册的信息,但在设计期间不显示这些信息,表示该组件会检查delphi的ide 环境,解决办法就是让自己的exe带上ide的信息;组件检查ide的办法无非就是使用api查找特定的…

执行POP和PUSH指令后,SS和SP的变化

我们知道push指令是将数据送入栈中,pop指令是将数据从栈顶取出来,8086CPU的入栈和出栈操作都是以字为单位的 比如说将10000H-1000FH这段内存当做栈使用 CPU是通过CS、IP中存放的段地址和偏移地址来知道当前要执行的指令,通过DS和[address]来…

python 文件遍历

1. import os,sys r raw_input(“type a directory name”) for root, dirs, files in os.walk(r): for f in files: print root, os.sep, f;2. import os,sys def walkdir(dirname): try: ls os.listdir(dirname) except: print ‘access deny’ else: for l in ls: temp o…

win7 php 上传文件,在LNMP原来的基础上,win7环境下如何上传PHP文件到Linux环境下...

首先,下载一个WINSCP客户端连接主机后,上传文件到自己的保存目录接着进入数据库添加我们的数据库mysql -uroot -p //这个是进入mysql的命令,但是要是你没有加 ln -s /usr/local/mysql/bin/mysql /usr/bin 的话就要输入下面那一行/usr/loc…

HDFC的完整形式是什么?

HDFC:住房发展金融公司 (HDFC: Housing Development Finance Corporation) HDFC is an abbreviation of Housing Development Finance Corporation. It is a well-known housing expansion finance corporation of India which largely makes available housing loa…

将10000H-1000FH这段空间当做栈,初始状态栈是空的,设置AX=001AH,BX=001BH,利用栈,交换AX和BX的数据

程序: mov ax,1000H mov ss,ax mov sp,0010H;设置AX和BX的值 mov ax,001AH mov bx,001BH;压栈 push ax push bx;出栈 pop ax pop bx解释: 在8086中,段寄存器不能直接传值,要通过一般寄存器,所以先将值传到ax中&#x…

利用WM_CTLCOLOR消息实现编辑控制(Edit Control)的文本与背景色的改变

(Abbey发表于2004-2-1 1:48:45)首先要明白:WM_CTLCOLOR是一个由控制(Control)发送给它父窗口的通知消息(Notification message)。实现步骤:生成一个标准的单文档应用程序框架,假设应用程序的名称为Color。我将利用它的About对话框做示范。在A…

fckeditor文件管理器添加查看、修改、删除文件功能[php]修正下载地址

2009年8月25日 由于PHP存在中文目录读取问题,将延期发布。2009年9月3日 正式发布:查看、修改、删除文件功能的fckeditor 2.6.4.1相关图片请查看:http://home.blueidea.com/attachment/200909/1/336696_1251813604S01b.gif相关其它&#xff1a…

php having,having方法

having方法1、对分组统计的结果,进行筛选如果将分分组查询的结果看成一张表的话,having方法类似where语句的功能2、源码:/thinkphp/library/think/db/Query.php/*** 指定having查询* access public* param string $having having* return $th…

合并排序算法排序过程_合并排序| 用于大型输入的最佳排序算法之一

合并排序算法排序过程What is sorting? 什么是分类? Sorting allows us to process our data in a more organized and efficient way. It makes searching easy as it will now take less time to search for a specific value in a given sorted sequence with …

Linux:jumpserver介绍(1)

官方网站 JumpServer - 开源堡垒机 - 官网https://www.jumpserver.org/ JumpServer 是广受欢迎的开源堡垒机,是符合 4A 规范的专业运维安全审计系统。JumpServer 帮助企业以更安全的方式管控和登录所有类型的资产,实现事前授权、事中监察、事后审计&…

对一个简单汇编程序分析

程序: assume cs:codesgcodesg segmentmov ax,0123Hmov bx,0456Hadd ax,bxadd ax,axmov ax,4c00Hint 21Hcodesg endsend伪指令: 伪指令是写给编译器看的,CPU不会执行,在源程序中,包括两种指令,一个是…

datagrid 页眉合并

http://www.codeproject.com/aspnet/MergeDatagridHeader.asp private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e){//产生序号,合并单元格if(e.Item.ItemIndex!-1){e.Item.Cells[1].TextConvert.ToString(e.Item.ItemIndex1);//}else{e…

劈尖等厚干涉条纹matlab,劈尖等厚干涉实验中,k=0级的干涉条纹是条纹,与k级暗条纹对应的空气薄膜的厚度为...

劈尖等厚干涉实验中,k0级的干涉条纹是条纹,与k级暗条纹对应的空气薄膜的厚度为答:暗,kλ/2spampython 编程\nprint(spam[-6:-4])是否报错?(是:则填写报错原因,否:则填写输出结果)答&…

使用OpenCV python模块读取图像并将其另存为灰度系统

In Python, we can use an OpenCV library named cv2. Python does not come with cv2, so we need to install it separately. 在Python中,我们可以使用名为cv2的OpenCV库 。 Python没有随附cv2 ,因此我们需要单独安装它。 For Windows: 对于Windows&a…

loop指令

功能:循环 格式:loop 标号 执行过程: cxcx-1判断cx的值,不为0则转至标号处执行程序,如果为0则向下执行 从上面的执行过程可以看出,cx存放的是循环次数 举个例子:实现2的12次方 assume cs:co…

不知不觉中学会做一个有主见的人

今天还是经历了不少事情,首先中午定好了聚会吃饭的地方,通知了以前班里的同学。事情解决了觉得心里很开心。中午不睡觉去人文馆准备听微软的宣讲会。等到三点多稀稀疏疏的几个人在那里,一个保安跑进来说取消了。埃,居然不通知。虽…

C# datetime 操作

C# datetime 操作 //C#里内置的DateTime基本上都可以实现这些功能,巧用DateTime会使你处理这些事来变轻松多了 //今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一…

php 子网掩码正则,验证子网掩码正则表达式代码范例

验证子网掩码正则表达式代码实例:在实际应用中可能验证子网掩码合法性的需求并不多,但并不能够保证绝对没有,下面就分享一段能够实现此功能的代码实例,希望能够给需要的朋友带来一定的帮助,代码如下:function checkMask(mask){var…