Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy<T>.asSequence returns infinite sequence #1

Closed
ilya-g opened this issue Jan 14, 2019 · 2 comments
Closed

Lazy<T>.asSequence returns infinite sequence #1

ilya-g opened this issue Jan 14, 2019 · 2 comments

Comments

@ilya-g
Copy link

ilya-g commented Jan 14, 2019

asSequence for Lazy being implemented this way

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) }
@ivan-moto
Copy link
Owner

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.

ivan-moto added a commit that referenced this issue Jan 15, 2019
@ivan-moto
Copy link
Owner

I fixed that here: c99437a

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants