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和偏函数] ... 
随机推荐
- window_x64微信小程序环境搭建
			所需文件地址如下: http://pan.baidu.com/s/1nv0IHhn(ylk7) 1.下载微信开发工具0.7.0_x64 安装完成后,打开程序,进行微信扫码登录 2.下载微信开发工具 ... 
- Hbase0.98.4/Hadoop2.4.1整合小结【原创】
			设定hbase的数据目录,修改conf/hbase-site.xml <configuration> <property> <name>hbase.cluster. ... 
- 架构模式之REST架构
			直至今日,分布式系统(Distributed System)已经取得了大规模的应用,特别是Web的发展,已经给软件开发带来了翻天覆地的变化,这一点已经毋庸置疑了. 构建分布式系统常用的技术通常就是使用 ... 
- 一步步教你搭建VS环境下用C#写WebDriver脚本
			一步步教你搭建VS环境下用C#写WebDriver脚本http://www.automationqa.com/forum.php?mod=viewthread&tid=3529&fro ... 
- Embedding Python in C
			http://codextechnicanum.blogspot.com/2013/12/embedding-python-in-c-converting-c.html //Make some vec ... 
- ODAC(V9.5.15) 学习笔记(十六)直接访问模式
			直接访问模式(Direct mode)是ODAC最大的特色之一,即不需要安装Oracle客户端,ODAC越过了OCI(Oracle Call Interface ),使用TCP/IP协议就可以直接与O ... 
- 验证 Xcode 是否来自正规渠道
			由于最近的 Xcode Ghost 事件的发生,所以我们有必要在安装完 Xcode 时验证其是否来自正规渠道. 在终端系统上运行以下命令启用检测: spctl --assess --verbose ... 
- QImage 图像格式小结
			原地址:http://tracey2076.blog.51cto.com/1623739/539690 嗯,这个QImage的问题研究好久了,有段时间没用,忘了,已经被两次问到了,突然有点解释不清楚, ... 
- [C#]LDAP验证用户名和密码
			测试环境:VS2008, NET Framework 3.5 公司打算改用LDAP来存储用户名和密码,现在用C#测试下如何能拿到LDAP中的用户名,并检测用户密码是否正确.即输入用户名和密码,可以检验 ... 
- Python 之 MySQL 操作库 lazy_mysql
			TOC Intro Installation Tutorial API Engine Pool Column Table Intro lazy_mysql 是一个非常简单易用,用来操作 MySQL 的 ... 
