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
    }
}

参照

posted @ 2016-09-18 15:22  SNYang  阅读(703)  评论(0编辑  收藏  举报