[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的用途的更多相关文章

  1. 学习Scala: 初学者应该了解的知识

    Scala开发参照清单 这里列出在开发一个Scala工程中需要参照的资料. 官网网站 http://www.scala-lang.org/ 文档网站 http://docs.scala-lang.or ...

  2. 我的Machine Learning学习之路

    从2016年年初,开始用python写一个简单的爬虫,帮我收集一些数据. 6月份,开始学习Machine Learning的相关知识. 9月开始学习Spark和Scala. 现在想,整理一下思路. 先 ...

  3. 【转载】 我的Machine Learning学习之路

    原文地址: https://www.cnblogs.com/steven-yang/p/5857964.html ------------------------------------------- ...

  4. 你需要了解的JS框架

    excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js       用途:构建数据统计图表,兼容多浏览器 ...

  5. 前端开发需要了解的JS插件

    excanvas.js/Chart.js/cubism.js/d3.js/dc.js/dx.chartjs.js/echarts.js/flot.js 用途:构建数据统计图表,兼容多浏览器 jquer ...

  6. Backbone vs AngularJS

    首先 Backbone 没有 AngularJS 那么容易上手. 而且作者并没有想让Backbone草根化的意思. Backbone 比喻成战斗机. 看上去更像是真正的MVC框架, model-vie ...

  7. Scala 中下划线的用途

    转载自:https://my.oschina.net/leejun2005/blog/405305 Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之 ...

  8. 浅谈 Scala 中下划线的用途

    Scala 作为一门函数式编程语言,对习惯了指令式编程语言的同学来说,会不大习惯,这里除了思维方式之外,还有语法层面的,比如 underscore(下划线)就会出现在多种场合,令初学者相当疑惑,今天就 ...

  9. Scala:函数式编程之下划线underscore

    http://blog.csdn.net/pipisorry/article/details/52913548 python参考[python函数式编程:apply, map, lambda和偏函数] ...

随机推荐

  1. mysql用shell建100多字段表并导入

    excel列超过160多个,导入时报错,把excel第一行另存为逗号分隔的csv文件,用shell建表 vim createTable.sh #!/bin/sh str="CA6430M,H ...

  2. [转载]堆排序(HeapSort) Java实现

    堆排序的思想是利用数据结构--堆.具体的实现细节: 1. 构建一个最大堆.对于给定的包含有n个元素的数组A[n],构建一个最大堆(最大堆的特性是,某个节点的值最多和其父节点的值一样大.这样,堆中的最大 ...

  3. ext 文档下载地址

    ext官方太鬼了,离线文档下载地址藏的太深了,找出来真不容易 http://docs.sencha.com/misc/guides/offline_docs.html

  4. Appium环境抢建

    原文:Appium环境抢建(for web browser test)Android SDKAppium安装 nodejs安装 Appium配置手机下载&运行测试项目Appium是Androi ...

  5. 译:用InnoSetup模块化安装依赖项

    译文出处:http://www.codeproject.com/Articles/20868/NET-Framework-Installer-for-InnoSetup 源文件下载:http://fi ...

  6. ruby中字符的转换

    1.将表格名转成class名 classify "book_comments".classify => "BookComment"   2.class名转 ...

  7. [CS231n-CNN] Training Neural Networks Part 1 : activation functions, weight initialization, gradient flow, batch normalization | babysitting the learning process, hyperparameter optimization

    课程主页:http://cs231n.stanford.edu/   Introduction to neural networks -Training Neural Network ________ ...

  8. 【转载】[JS]让表单提交返回后保持在原来提交的位置上

    有时候,在网页中点击了页面中的按钮或是刷新了页面后,页面滚动条又 会回到顶部,想看后面的记录就又要拖动滚动条,或者要按翻页键,非常不方便,想在提交页面或者在页面刷新的时候仍然保持滚动条的位置不变,最好 ...

  9. 轻量级IOC框架:Ninject (下)

    一,创建依赖链(Chains of Dependency) 当我们向Ninject请求创建一个类型时,Ninject会去检查该类型和其他类型之间的耦合关系.如果有额外的依赖,Ninject也会解析它们 ...

  10. MyBatis知多少(26)MyBatis和Hibernate区别

    iBatis和Hibernate之间有着较大的差异,但两者解决方案很好,因为他们有特定的领域.我个人建议使用MyBatis的,如果: 你想创建自己的SQL,并愿意维持他们. 你的环境是由关系数据模型驱 ...