Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试找到一种有效的方法来将列表汇总到所选索引。
var groupList : List[Int] = List(1,3,4,5)
如果我选择 indexOf(2),我能做些什么来实现该索引的总和?(1 + 3 + 4) ?
怎么样:
val idx = 2 groupList.take(idx+1).sum // res3: Int = 8
或者为了效率:
groupList.toIterator.take(idx+1) // res4: Iterator[Int] = non-empty iterator groupList.toIterator.take(idx+1).sum // res5: Int = 8