您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scala