Tag: 懒惰序列

如何在科特林无限和懒惰地循环列表?

我有一个directions列表,并希望find下一个方向,当我左转或右转。 这是我有的工作代码: enum class Turn { R, L } enum class Direction { N, E, S, W } val directionsInRightTurnOrder = listOf(Direction.N, Direction.E, Direction.S, Direction.W) private fun calculateNextHeading(heading: Direction, turn: Turn): Direction { val currentIndex = directionsInRightTurnOrder.indexOf(heading) var nextIndex = currentIndex + if (turn == Turn.R) 1 else -1 if (nextIndex >= directionsInRightTurnOrder.size) nextIndex = […]

Kotlin的Iterable和Sequence看起来完全一样。 为什么需要两种types?

这两个接口都只定义了一种方法 public operator fun iterator(): Iterator 文件说Sequence是懒惰的。 但是也不是可以Iterable懒惰(除非有一个Collection支持)?