zoukankan      html  css  js  c++  java
  • Scala _ [underscore] magic

    I started learning Scala a few days before. Initially i was annoyed by the use of too many symbols in Scala. Especially i was confused by the _ and its different meaning in different places. After a few days of research(aka search), i felt better and i started to love its syntax. Ok, lets see the usage of _ in Scala.

    "Alert" : "I am a Scala n00b"

    Pattern Matching

    In Scala, pattern matching is somewhat similar to java switch statement. But it is more powerful.

    def matchTest(x: Int): String = x match {
        case 1 => "one"
        case 2 => "two"
        case _ => "anything other than one and two"
      }

    In Scala, each selector will be matched with the patterns in the order they appear and the first match will be executed. _ acts like a wildcard. It will match anything. Scala allows nested patterns, so we can nest the _ also.Lets see another example that uses _ in nested pattern.

    expr match {
      case List(1,_,_) => " a list with three element and the first element is 1"
      case List(_*)  => " a list with zero or more elements "
      case Map[_,_] => " matches a map with any key type and any value type "
      case _ =>
      }

    Anonymous Functions

    Scala represents anonymous functions with a elegant syntax. The _ acts as a placeholder for parameters in the anonymous function. The _ should be used only once, But we can use two or more underscores to refer different parameters.

    List(1,2,3,4,5).foreach(print(_))
    List(1,2,3,4,5).foreach( a => print(a))

    Here the _ refers to the parameter. The first one is a short form of the second one. Lets look at another example which take two parameters.

    val sum = List(1,2,3,4,5).reduceLeft(_+_)
    val sum = List(1,2,3,4,5).reduceLeft((a, b) => a + b)

    There is a good post which explains the inner details of the above example.

    import

    In scala, _ acts similar to * in java while importing packages.

    // imports all the classes in the package matching
    import scala.util.matching._
    // imports all the members of the object Fun. (static import in java)
    import com.test.Fun._
    // imports all the members of the object Fun but renames Foo to Bar
    import com.test.Fun.{ Foo => Bar , _ }
    // imports all the members except Foo. To exclude a member rename it to _
    import com.test.Fun.{ Foo => _ , _ }

    Properties

    In scala, a getter and setter will be implicitly defined for all non-private var in a object. The getter name is same as the variable name and _= is added for setter name. We can define our own getters and setters. This is looking similar to Ruby getters and setters. Ok lets see an example which uses the getter and setters.

    class Test {
       private var a = 0
       def age = a
       def age_=(n:Int) = {
          require(n>0)
          a = n
       }
    }
    val t = new Test
    t.age = 5
    println(t.age)

    Functions

    Scala is a functional language. So we can treat function as a normal variable. If you try to assign a function to a new variable, the function will be invoked and the result will be assigned to the variable. This confusion occurs due to the optional braces for method invocation. We should use _ after the function name to assign it to another variable.

    class Test {
      def fun = {
        // some code
      }
      val funLike = fun _
    }
  • 相关阅读:
    【试水CAS-4.0.3】第07节_CASclient配置单点登录
    30分钟,让你彻底明白Promise原理
    【你离硅谷只差一步】网易中国创业家大赛项目火热征集中
    21分钟学会写编译器
    Android 模拟器下载、编译及调试
    GitLab 自动触发 Jenkins 构建
    微服务监控探索
    感觉要火!妹子实地采访网易猪厂程序员七夕怎么过
    延迟任务调度系统—技术选型与设计(下篇)
    使用QUIC
  • 原文地址:https://www.cnblogs.com/suanec/p/4987160.html
Copyright © 2011-2022 走看看