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和偏函数] ...
随机推荐
- Java中RSA非对称密钥加解密使用示例
一.简介: RSA加密算法是最常用的非对称加密算法,CFCA在证书服务中离不了它.RSA是第一个比较完善的公开密钥算法,它既能用于加密,也能用于数字签名.这个算法经受住了多年深入的密码分析,虽然密码分 ...
- C# vs MySql
MySqlHelper类 /// <summary> ///MySql操作类 /// </summary> public abstract class MySqlHelper ...
- Vim安装jedi-vim提示的一个错误
(仅为了提醒自己) 第一次的安装方法好像是通过 bundle安装的,好像是通过这个安装的并不是最新的版本,然后删除了通过下面的方法,最重要的是要执行 git submodule update --in ...
- 转:php park、unpark、ord 函数使用方法(二进制流接口应用实例)
在工作中,我也逐渐了解到park,unpark,ord对于二进制字节处理的强大. 下面我逐一介绍它们. park,unpark,ord这3个函数,在我们工作中,用到它们的估计不多. 我在最近一 ...
- 云极知客开放平台接口调用方法(C#)
云极知客为企业提供基于SAAS的智能问答服务.支持企业个性化知识库的快速导入,借助语义模型的理解和分析,使企业客户立即就拥有本行业的24小时客服小专家.其SAAS模式实现零成本投入下的实时客服数据的可 ...
- ubuntu关闭自动更新、打开 ubuntu 的 apport 崩溃检测报告功能
菜单栏点 系统 --->首选项 --->启动应用程序 --->更新提示 前面的钩打掉 关闭即可 =========== 打开 ubuntu 的 apport 崩溃检测报告功能 ...
- svn diff excel
https://github.com/solq360/compareExcel jdk 1.7 可自己编译1.6 每个sheet第一行不允许为空 SVN对比excel差异 适合策划.财务职业使用 sv ...
- How to implement an algorithm from a scientific paper
Author: Emmanuel Goossaert 翻译 This article is a short guide to implementing an algorithm from a scie ...
- [git]查看某一行代码是谁写的
git blame filename,'blame'意思为责怪!哈哈哈. 就会列出来每行的修改纪录.你可以通过行数或者代码来查看,是谁的锅!
- [python]pip常用命令(转载)
用阿里云服务器,使用pip安装第三方库的时候卡的要死.所以我就想pip能不能安装本地的包. 找到了这篇博客: http://me.iblogc.com/2015/01/01/pip%E5%B8%B8% ...