惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - cindy_zl

SAP SD销售与分销模块 Agent认知框架ReAct&Plan RAG系统优化 RAG检索优化 RAG架构 SAA-C02-架构师助理级-英文版-Q1 scala-数组 navicat mysql创建存储过程报错 安装完kali出现中文乱码 Spark SQL是处理结构化的数据 SPARK SQL ERROR: Detected cartesian product for INNER join between logical plans报错解决方法 scala的lazy关键字 scala集合三大类(seq序列,set集,map映射)——map映射 scala集合三大类(seq序列,set集,map映射)——set集合 scala集合三大类(seq序列,set集,map映射)——list序列 scala元组及拉链操作 scala的map映射 scala数组 scala的基础数据类型&if条件表达式&for循环
scala方法和涵数的声明以及方法转换成涵数
cindy_zl · 2019-09-17 · via 博客园 - cindy_zl

scala方法和涵数的声明以及方法转换成涵数:

方法声明:

scala> def m1(x:Int, y:Int) : Int = x + y

m1: (x: Int, y: Int)Int

scala> m1(3,4)

res9: Int = 7

x + y为涵数体 

涵数声明:

scala> val f1 = (x: Int, y: Int) => x + y

f1: (Int, Int) => Int = <function2>

(Int, Int)参数类型

黄色是返回类型,后面的 <function2>表示两个参数的一个涵数,x + y为涵数体

scala> def m2(f:(Int,Int)=>Int) = f(3,4)

m2: (f: (Int, Int) => Int)Int 定入的一个方法m2并且方法的参数是一个涵数类型的参数,后面是方法的实现体

scala> val f1=(x:Int,y:Int) => x+y

f1: (Int, Int) => Int = <funtion2>定义一个有两个参数的涵数 f1,做为方法m2的参数传入方法体

scala> m2(f1) 方法里面传入涵数名调用(即scala可以将一个涵数当做参数进行传入)

res10: Int = 7

方法转换为一个涵数;

scala> def m1(x:Int,y:Int):Int=x+y         //定义一个两个参数为int类型且返回类型也是一个int的一个方法m1

m1: (x: Int, y: Int)Int

scala> val f1 = m1 _                        //将m1转换成涵数据并用 f1 接收  方法名+空格+下划线

f1: (Int, Int) => Int = <function2>

然后就可以调用 m2(f1)

scala> m2(f1)            传入转换过的涵数

res12: Int = 7

scala> m2(m1)           直接传入方法也没有报错是因为scala内部自动隐式转换成了涵数后当做m2方法的参数传入的,这有个隐式转换过程

res13: Int = 7