# lazySequence

**URL:** <https://discuss.kotlinlang.org/t/lazysequence/28235>\
**Category:** Language Design\
**Created:** [February 27, 2024, 4:46pm UTC](https://discuss.kotlinlang.org/t/lazysequence/28235 "2024-02-27T16:46:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [February 27, 2024, 4:46pm UTC](https://discuss.kotlinlang.org/t/lazysequence/28235/1 "2024-02-27T16:46:06Z")

</div>

We need something like this:

```kt
private val fibSeq = lazySequence(1) { i, f ->
    f(i - 1) + f(i - 2)
}

fun fib(i: Int) =
    fibSeq[i - 2]

```

An example implementation:

```kt
interface IndexableSequence<T>: Sequence<T> {
    operator fun get(index: Int): T
}

fun <T> lazySequence(z: T, f: (Int, (Int) -> T) -> T): IndexableSequence<T> =
    object : IndexableSequence<T> {
        val map = mutableMapOf(0 to z)
        
        fun comp(iIn: Int): T {
            val i = max(0, iIn)
            return map[i] ?: f(i, ::comp).also { map[i] = it }
        }
            
        override fun get(index: Int) = comp(index)
        
        override fun iterator(): Iterator<T> =
        	object : Iterator<T> {
                override fun hasNext() = true
				
                private var i = 0
                
                override fun next(): T =
                	comp(i ++)
            }
    }

```

If something like that already exists, tell me. I don’t know every function inside of the Kotlin standard library.

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [February 27, 2024, 5:35pm UTC](https://discuss.kotlinlang.org/t/lazysequence/28235/2 "2024-02-27T17:35:03Z")

</div>

I think you can use your implementation, is there something wrong with it?

Sequences are lazy, so probably the name `lazySequence` isn’t the best choice. Additionally from your example you don’t much need this object to be a sequence, you just need `get(index: Int)`.

---

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [February 27, 2024, 5:39pm UTC](https://discuss.kotlinlang.org/t/lazysequence/28235/3 "2024-02-27T17:39:31Z")

</div>

Well yes, I can use this but I was talking about having something similar in the stdlib.  
I didn’t come up with a better name for it than `lazySequence`.  
Yeah in this specific example, I don’t need it to be a sequence.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [February 27, 2024, 6:03pm UTC](https://discuss.kotlinlang.org/t/lazysequence/28235/4 "2024-02-27T18:03:44Z")

</div>

I don’t think you will find anything better in the stdlib. This is a very specialized util, I can’t think of too many use cases for it.

Also, I think it doesn’t have too much in common with sequences. It seems more like a lazy collection to me.
