Scala中的占位符 _
2017-01-15 19:50
134 查看
我们都知道scala的语法十分的简洁,有些语法简洁的让人找不到北。话不多说,看看占位符 _
下面就解释下为什么:
Note: this method underlies the implementation of most other bulk operations. Subclasses should re-implement this method if a more efficient implementation exists.
这是鼠标悬停在 foreach 看到的解释,foreach 接受一个函数参数,该函数传入类型Char ,返回空。这个应该能懂的!
"hello".foreach((x:Char)=>println(x)) 这种写法是在foreach里面构造一个匿名函数
"hello".foreach(println(_)) 这种写法本质上也是在构造一个匿名函数,Scala语法规定 匿名函数,如果 =》右边 只出现一次函数参数,切记只出现一次,(x:Char)就可以略去
"hello".foreach(println) 这种写法相信大家都懂,就是把println函数传递进去
我也是今天刚懂的,记录一下,不知道理解的对错
下面就解释下为什么:
override def foreach[U](f: Char => U): Unit
Applies a functionfto all elements of this string.
Note: this method underlies the implementation of most other bulk operations. Subclasses should re-implement this method if a more efficient implementation exists.
这是鼠标悬停在 foreach 看到的解释,foreach 接受一个函数参数,该函数传入类型Char ,返回空。这个应该能懂的!
"hello".foreach((x:Char)=>println(x)) 这种写法是在foreach里面构造一个匿名函数
"hello".foreach(println(_)) 这种写法本质上也是在构造一个匿名函数,Scala语法规定 匿名函数,如果 =》右边 只出现一次函数参数,切记只出现一次,(x:Char)就可以略去
"hello".foreach(println) 这种写法相信大家都懂,就是把println函数传递进去
我也是今天刚懂的,记录一下,不知道理解的对错
相关文章推荐
- rdd,scala中的占位符“_”
- scala占位符_的用法
- Scala基础—集合函数式编程示例(占位符的使用示例)
- scala memched 集成
- scala 排序算法 希尔排序
- Play2 for Scala中文文档 – 10. 使用anorm操作数据库
- scala实战书籍
- windows下使用sbt搭建Scala开发环境
- scala学习第六天
- scala的隐式转换之二
- scala第九天Class
- 快学scala习题(三)
- Idea+maven+scala创建过程详解
- Scala坑一(基础)
- Scala 的 yield 例子 (for 循环和 yield 的例子)
- eclipse构建maven+scala+spark工程 转载
- scala----计数器zipWithIndex
- Scala坑二(控制结构和函数)
- scala筛选460亿条记录的hive表
- Scala坑三(数组相关操作)