Scala _ [underscore] magic
2015-11-22 23:47
183 查看
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
In Scala, each selector will be matched with the patterns in the order they appear and the first match will be executed.
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.
There is a good post which explains the inner details of the above example.
_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 _ }
相关文章推荐
- ocr识别中的图像分割方法小结
- 面向对象
- QListWidget和QListWidgetItem的简单使用
- 进一步封装ListView,实现下拉刷新和分页刷新的功能
- Freemarker常用技巧(二)
- Nmap / NetCat(nc) / 网络安全工具
- hihocoder #1128 : 二分·二分查找
- GCD的常用方法
- Freemarker常用技巧(一)
- HOJ-1011 / POJ-1589
- Mvc 中ViewBag Model 查找不到解决
- GCD的基本使用
- 栈 (Stack) 的C++实现(链表方法)
- 立体视觉——固定窗口的视差图计算
- 图片轮播器
- freemarker常用的基本命令
- 给php程序员的40条建议 优化你的php代码(一)【转载】
- 小米4
- Android中的五大布局
- GCD介绍