Scala匿名函数语法
来源:互联网 发布:js二级下拉菜单 编辑:程序博客网 时间:2024/05/21 09:27
Scala提供了相对轻量级的语法来定义匿名函数。下面表达式创建了一个整数加1函数。
(x: Int) => x + 1
这是下面匿名类定义的简写:
new Function1[Int, Int] { def apply(x: Int): Int = x + 1}
也可以定义带多个参数的函数:
(x: Int, y: Int) => "(" + x + ", " + y + ")"
或者不带参数:
() => { System.getProperty("user.dir") }
还有一种非常轻量级的方式来写函数类型。下面是上面定义的三个函数的类型:
Int => Int(Int, Int) => String() => String
这个语法是下面类型的简写:
Function1[Int, Int]Function2[Int, Int, String]Function0[String]
下面展示了如何使用开始那个匿名函数。
object AnonymousFunction { /** * Method to increment an integer by one. */ val plusOne = (x: Int) => x + 1 /** * Main method * @param args application arguments */ def main(args: Array[String]) { println(plusOne(0)) // Prints: 1 }}
0 0
- Scala匿名函数语法
- Scala的匿名函数!
- Scala 匿名函数
- Scala 函数与匿名函数
- Scala函数和匿名函数
- Scala 闭包 匿名函数
- scala => 用法 匿名函数
- !!scala 高阶函数,匿名函数
- Scala 的 "匿名函数"与 "闭包"!
- 白话scala系列五 匿名函数分析
- Scala 学习笔记(五)------定义匿名函数
- 【scala 函数定义和调用】Scala的函数调用:普通函数、匿名函数、柯里化函数
- 匿名函数的语法与使用
- Scala学习第六弹:函数与匿名函数
- Scala匿名类的函数是private还是public?
- scala语法
- Scala 语法
- scala语法
- Oracle AWR报告提取方法
- MySql 1045 access denied for user 'root'@'localhost' using password yes错误解决方法
- spark厦大---Word2Vec--spark.ml
- 浅谈C#中virtual和abstract的区别
- 生成器 Generators
- Scala匿名函数语法
- 二五、vue仿百度搜索框
- MySQL ibdata1文件迁移
- 第5章 定时器Timer
- spark厦大---标签和索引的转化:StringIndexer- IndexToString-VectorIndexer
- JS中Location使用详解
- Java基础编程题
- Activity过渡动画(overridePendingtransition()的使用)
- expdp/impdp 参数说明,中英对照