Thread
#stdlib
    Rob Elliot

    Rob Elliot

    7 months ago
    Candidate mirror function to
    fun <T> Iterable<T>.withIndex(): Iterable<IndexedValue<T>>
    to include in the stdlib:
    fun <T> Iterable<IndexedValue<T>>.withoutIndex(): List<T> = map(IndexedValue<T>::value)
    e

    ephemient

    7 months ago
    withIndex provides a indexed view, not a copy, while map copies eagerly
    Rob Elliot

    Rob Elliot

    7 months ago
    I'm sure there's a better implementation, it's the concept I'm suggesting. Perhaps I should just have gone with the signature:
    fun <T> Iterable<IndexedValue<T>>.withoutIndex(): Iterable<T>
    Dominaezzz

    Dominaezzz

    7 months ago
    Feels a bit specific. Like what if the indexes are wrong.
    Rob Elliot

    Rob Elliot

    7 months ago
    What if they are? It's just to discard them. Iteration order would be unchanged.
    marcinmoskala

    marcinmoskala

    7 months ago
    fun <T> Iterable<IndexedValue<T>>.withoutIndex(): Iterable<T> = UnindexedIterable(this)
    
    internal class UnindexedIterable<T>(private val iterable: Iterable<IndexedValue<T>>) : Iterable<T> {
        override fun iterator(): Iterator<T> = UnindexedIterator(iterable.iterator())
    }
    
    internal class UnindexedIterator<out T>(private val iterator: Iterator<IndexedValue<T>>) : Iterator<T> {
        override fun hasNext(): Boolean = iterator.hasNext()
        override fun next(): T = iterator.next().value
    }