Scala underscore的用途
[comment]: # Scala underscore的用途
_ 的用途
// import all
import scala.io._
// import all, but hide Codec
import scala.io.{Codec => _, _}
// import all, but rename Codec as CodeA
import scala.io.{Codec => CodeA, _}
// import all from an object
import scala.io.Codec._
object Main {
// Higher kinded type parameter
import scala.language.higherKinds
class CollectionAdapter[T[_]] {
def getCollectionFromValue[A](a: A, f: A => T[A]): T[A] = {
f(a)
}
def intToList(a: Int): List[Int] = {
List(a)
}
}
def main(args: Array[String]): Unit = {
// Higher kinded type parameter
val adapter = new CollectionAdapter[List]()
println(adapter.getCollectionFromValue(1, adapter.intToList))
// output: List(1)
// Initialize a variable with default value
var i:Int = 0 // _ cannot be used in a function
println(i)
// output: 0
// Partial function application, Assign a function rather than run it.
def fun1(): Unit = {println("fun1 is invoked.")}
def obj1 = fun1 // run fun1 and return the result to obj1
// output: fun1 is invoked.
// output: ()
def fun2 = fun1 _ // assign fun1 to fun2
println(fun2)
// output: <function0>
// Anonymouse function, scala will infer it base on the context.
val list1 = (1 to 10)
println(list1.filter(_ > 5))
// output: Vector(6, 7, 8, 9, 10)
// equivalent to:
println(list1.filter(a => a > 5))
// output: Vector(6, 7, 8, 9, 10)
// setter function
class A {
private var _count = 0
// getter
def count = {_count}
// setter
def count_= (n: Int) = {_count = n}
// bang ??
def bang_! (n: Boolean) = {5}
}
val a = new A
a.count = 5
println(a.count)
// output: 5
// Pattern match
def matchTest(x: Any): String = x match {
case 1 => "one"
case 2 => "two"
case List(0, _, _) => "a list with three elements and the first element is 0"
case List(_*) => "a list with zero or more elements"
case _: Map[_, _] => "matches a map with any key type and any value type"
case _ => "anything else"
}
// anonymous variable. (or we can say ignored variable)
for (_ <- 1 to 2) {
println("hi")
}
// Sequence list2 is passed as multiple parameters to f(a: T*)
def fun3(a: Int*) = { a.map(println(_)) }
fun3(1 to 3: _*)
// output:
// 1
// 2
// 3
// Access tuple
var t = new Tuple3(1, "Two", "Three")
println(t._2)
// output: Two
}
}
参照
Scala underscore的用途的更多相关文章
- 学习Scala: 初学者应该了解的知识
Scala开发参照清单 这里列出在开发一个Scala工程中需要参照的资料. 官网网站 http://www.scala-lang.org/ 文档网站 http://docs.scala-lang.or ...
- 我的Machine Learning学习之路
从2016年年初,开始用python写一个简单的爬虫,帮我收集一些数据. 6月份,开始学习Machine Learning的相关知识. 9月开始学习Spark和Scala. 现在想,整理一下思路. 先 ...
- 【转载】 我的Machine Learning学习之路
原文地址: https://www.cnblogs.com/steven-yang/p/5857964.html ------------------------------------------- ...
- 你需要了解的JS框架
excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 ...
- 前端开发需要了解的JS插件
excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...
- Backbone vs AngularJS
首先 Backbone 没有 AngularJS 那么容易上手. 而且作者并没有想让Backbone草根化的意思. Backbone 比喻成战斗机. 看上去更像是真正的MVC框架, model-vie ...
- Scala 中下划线的用途
转载自:https://my.oschina.net/leejun2005/blog/405305 Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之 ...
- 浅谈 Scala 中下划线的用途
Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...
- Scala:函数式编程之下划线underscore
http://blog.csdn.net/pipisorry/article/details/52913548 python参考[python函数式编程:apply, map, lambda和偏函数] ...
随机推荐
- 标准 DateTime 格式字符串
标准 DateTime 格式字符串 MSDN 标准 DateTime 格式字符串包含一个标准 DateTime 格式说明符字符,该字符表示自定义 DateTime 格式字符串.格式字符串最终定义由格式 ...
- System.Diagnostics.Stopwatch
System.Diagnostics.Stopwatch 注意:此类在 .NET Framework 2.0 版中是新增的.MSDN Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量 ...
- Promising Linking
Future/Promise 执行逻辑 scala Future 有几个要点,第一个是 tryAwait 需要借助 CowndownLatch 实现,第二个是可以在 Promise 挂载回调函数 首先 ...
- Ubuntu14.04手动创建桌面快捷方式
如果是系统自带的程序,默认的桌面图标放在 /usr/share/applications/下面,可以直接将对应的图标放到当前用户的~/Desktop/目录下即可 如果是从网上下载已编译的二进制文件(e ...
- React Native ——实现一个简单的抓取github上的项目数据列表
/** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; var React ...
- PostgreSQL表空间、模式、表、用户/角色之间的关系
PostgreSQL表空间.模式.表.用户/角色之间的关系是本文我们主要要介绍的内容,表空间,数据库,模式,表,用户,角色之间的关系到底是怎样的呢?接下来我们就开始介绍这一过程. 实验出角色与用户的关 ...
- XCActionBar 「Xcode 中的 Alfred」
下载地址:https://github.com/pdcgomes/XCActionBar 基本命令: (1)「command+shift+8」或者双击「command」键可以打开「动作输入框窗口」 ( ...
- Unix sed实用教程系列目录[已完成]
本系列文章已经译完了,译自awk-sed@theunixschool,收获颇丰,作者没有讲明白的我做了补充,并且尝试讲的更清楚,整理成系列索引,方便学习,开篇非译文,是我为了方便后面的理解写的一篇,感 ...
- ArchLinux 下架设PPTPD VPN服务
直接上命令吧: 安装: pacman -Sy pacman -S pptpd 配置: vim /etc/pptpd.conf option /etc/ppp/options.pptpd stimeou ...
- 使用word和pdf进行仿书编辑的经验
一.问题的提出: 一本书扫描好,要将书中的图片转换为文字版的word文档.二.问题的分析: 1.文字的提取 2.文字的编排三.问题的解决 1.如果用的是Adobe Acroba ...