加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

"=>" 在scala 中的用法

发布时间:2020-12-16 09:47:34 所属栏目:安全 来源:网络整理
导读:例一 :《Spark大数据处理技术》程序2-25 val compareElement:((String,Int),(String,Int)) = (String,Int) = (val1,val2) = { if (val1._2 = val2._2){ ?val1 } else { ?val2 } } 注释 : 1、用val定义函数 2、 compareElement函数是匿名函数 3、 ((String,

例一:《Spark大数据处理技术》程序2-25

val compareElement:((String,Int),(String,Int)) => (String,Int) = (val1,val2) => {

if (val1._2 >= val2._2){

?val1

} else {

?val2

}

}

注释

1、用val定义函数

2、compareElement函数是匿名函数

3、((String,Int)

compareElement函数的函数类型声明,左边是输入参数类型,右边是方法返回值类型

4、(val1,val2) => {......}

左边是参数,右边是函数实现体


例二

网址:http://zhouchaofei2010.iteye.com/blog/2251731

目前知道的基本和匿名函数定义和函数类型声明有关,以及在case语句中的使用

=>?
1、 匿名函数定义, 左边是参数 ?右边是函数实现体 ?(x: Int)=>{}
2、函数类型的 声明,左边是参数类型,右边是方法返回值类型 ? ? ? ? ? ? ? ?(Int)=>(Int)
3、 By-name-parameter ? ? ?f(p :=>Int)
? ? ? ??? ? 与2的区别 参考:scala By-name-parameter 和 Function type的区别
4、 case ?语句中? case x => y
?

?

不过stackoverflow?上关于这方面的讨论挺多的,故在此记录一下

比如来自?http://stackoverflow.com/questions/6951895/what-does-and-mean-in-scala?的一位的回复:

?

=>?has several meanings in Scala,all related to its mathematical meaning as implication.

  • In a value,it introduces a function literal,or?lambda. e.g. the bit inside the curly braces in?List(1,2,3).map { (x: Int) => x * 2 }(匿名函数定义

  • In a type,with symbols on both sides of the arrow (e.g.?A => T,?(A,B) => T,B,C) => T,etc.) it's sugar for?Function<n>[A[,...],T],that is,a function that takes parameters of type?A[,B...],and returns a value of type?T.(函数类型定义

    • Empty parens on the left hand side (e.g.?() => T) indicate that the function takes no parameters (also sometimes called a "thunk");

    • Empty parens on the right hand side denote that it returns?()—the sole value of type?Unit,whose name can also be written?()—confused yet? :)

      A function that returns Unit is also known as a?procedure,normally a method that's called only for its side effect.

  • In the type declaration for a method or function parameter,with no symbol on the left hand side (e.g.?def f(param: => T)) it's a "by-name parameter",meaning that is evaluated every time it's used within the body of the function,and not before. Ordinary "by-value" parameters are evaluated before entry into the function/method.(函数类型定义

  • In a?case?clause,they separate the pattern (and optional guard) from the result expression,e.g.?case x => y.(case 语句

http://stackoverflow.com/questions/6951895/what-does-and-mean-in-scala

http://stackoverflow.com/questions/7888944/scala-punct

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读