You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fun <T> Lazy<T>.asSequence(): Sequence<T> = object : Iterator<T> {
override fun hasNext(): Boolean = true
override fun next(): T = value
}.asSequence()
returns a sequence that can be iterated only once, and once iterated it returns the value indefinitely, because hasNext is always true.
The correct implementation would be:
fun <T> Lazy<T>.asSequence(): Sequence<T> = sequence { yield(value) }
The text was updated successfully, but these errors were encountered:
The original implementation is actually what I intended: an infinite sequence of the given Lazy’s value. But now that you mention it, other people reading that snippet will likely have the same expectation you have: that the function returns a single value Sequence. Thanks for bringing this to my attention. I will update it.
asSequence
forLazy
being implemented this wayreturns a sequence that can be iterated only once, and once iterated it returns the value indefinitely, because
hasNext
is alwaystrue
.The correct implementation would be:
The text was updated successfully, but these errors were encountered: