前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Scala underscore的用途

Scala underscore的用途

作者头像
绿巨人
发布2018-05-16 17:43:52
1.5K0
发布2018-05-16 17:43:52
举报
文章被收录于专栏:绿巨人专栏绿巨人专栏

_ 的用途

代码语言:javascript
复制
// 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
    }
}

参照

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-09-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • _ 的用途
  • 参照
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档