Tag: 懒惰序列

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

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

如何在Kotlin无限和懒惰地循环列表?

我有一个directions列表,并希望找到下一个方向,当我左转或右转。 这是我有的工作代码: 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示例

我想学习Kotlin,并通过try.kotlinlang.org上的示例进行工作 我无法理解一些示例,特别是Lazy属性示例: https : //try.kotlinlang.org/#/Examples/Delegated%20properties/Lazy%20property/Lazy%20property.kt /** * Delegates.lazy() is a function that returns a delegate that implements a lazy property: * the first call to get() executes the lambda expression passed to lazy() as an argument * and remembers the result, subsequent calls to get() simply return the remembered result. * If you want thread […]

Kotlin序列函数未解决的参考

Kotlin 1.0.0 IDEA 2016.1 我发现了一些用于创建序列的新sequence函数(不再称为stream )的引用。 JetBrains博客提供了以下示例: val elements = sequence(1, { x -> x + 1}) val elements = listOf(1, 2, 3, 4).sequence() AgileWombat博客给出了类似的例子。 val squares = sequence(1) {it + 1}.map {it * it} 但是,当我尝试使用这些示例时,无论是在REPL还是在IDE中(IDEA 2016.1),我都会得到以下结果: >>> val squares = sequence(1) {it + 1}.map {it * it} error: unresolved reference: sequence val squares = […]