Scala 深入浅出实战经典第 83讲:Scala中List的实现内幕源码揭秘
2015-09-11 05:53
302 查看
List中的take是用ListBuffer实现的:
2.10.x 版本
但是在2.11.x版本中不是:
override def take (n: Int): List[ A] = if (isEmpty || n <= 0) Nil else {
val h = new ::( head, Nil )
var t = h
var rest = tail
var i = 1
while ({ if (rest .isEmpty) return this ; i < n}) {
i += 1
val nx = new ::(rest .head, Nil)
t.tl = nx
t = nx
rest = rest .tail
}
h
}
final case class ::[B](override val head : B, private [scala] var tl: List[B]) extends List[B]
声明为var是可以让ListBuffer操作
信息来源于 DT大数据梦工厂,微信公众号:DT_Spark
视频地址:http://edu.51cto.com/lesson/id-71363.html
2.10.x 版本
但是在2.11.x版本中不是:
override def take (n: Int): List[ A] = if (isEmpty || n <= 0) Nil else {
val h = new ::( head, Nil )
var t = h
var rest = tail
var i = 1
while ({ if (rest .isEmpty) return this ; i < n}) {
i += 1
val nx = new ::(rest .head, Nil)
t.tl = nx
t = nx
rest = rest .tail
}
h
}
final case class ::[B](override val head : B, private [scala] var tl: List[B]) extends List[B]
声明为var是可以让ListBuffer操作
信息来源于 DT大数据梦工厂,微信公众号:DT_Spark
视频地址:http://edu.51cto.com/lesson/id-71363.html
相关文章推荐
- Windows下Scala环境搭建
- Windows7下安装Scala 2.9.2教程
- XML 文件解析--含Unicode字符的XML文件
- 分分钟掌握快速排序(Java / Scala 实现)
- Scala极速入门
- Spark初探
- Scala实现REST操作
- Scala method call syntax
- 关于Scala多重继承的菱形问题
- Scala 高阶函数(high-order function)剖析
- Spray.io搭建Rest服务
- Spray.io搭建Rest — 支持Twirl模板并部署
- 搭建hadoop/spark集群环境
- zeppelin入门使用
- ScalaMP ---- 模仿 OpenMp 的一个简单并行计算框架
- 用Scala实现延迟计算
- SBT学习 [持续更新...]
- Scala创建新的控制结构
- Scala: 一次命令式到函数式的重构
- 浅谈Scala的特质(trait)