# Does Kotlin have something similar to RxJava scan?

**URL:** <https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275>\
**Category:** Uncategorized\
**Created:** [April 27, 2015, 5:12am UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275 "2015-04-27T05:12:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lakedaemon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lakedaemon/32/1421_2.png) [@Lakedaemon](https://discuss.kotlinlang.org/u/Lakedaemon)\
**Post date:** [April 27, 2015, 5:12am UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/1 "2015-04-27T05:12:17Z")

</div>

I have a simple list (or sequence) of Pair(charSequence, Int)

(C0, 0), (C1, 1), (C2, 2), (C3, 3), …

I would like to accumulate the ints and produce  
(C0, 0), (C1, 1), (C2, 3), (C3, 6)

It’s quite easy to do in imperative style, but what about fonctionnal style :  
Is there a way to do that with an operator on lists/sequences in Kotlin ? with Java8 and streams ?

It is possible to do in RxJava with the operator scan  
[http://reactivex.io/documentation/operators/scan.html](http://reactivex.io/documentation/operators/scan.html)

Also, I often wonder if the Kotlin operators on iterable/sequences in the std lib are not a bit lacking compared to RxJava operators/transformers.  
The RxJava api seems a lot more full featured while not being bloated

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [February 20, 2017, 10:19pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/2 "2017-02-20T22:19:51Z")

</div>

I second the need for this. Basically scan is like fold except that instead of giving you just the last value it gives you the seed and all intermediate values

---

<div class="post-metadata">

**Author:** ![bulatmukhutdinov](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@bulatmukhutdinov](https://discuss.kotlinlang.org/u/bulatmukhutdinov)\
**Post date:** [December 13, 2018, 10:34am UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/3 "2018-12-13T10:34:50Z")

</div>

I am third. It would really useful function

---

<div class="post-metadata">

**Author:** ![tommot1985](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tommot1985/32/5785_2.png) [@tommot1985](https://discuss.kotlinlang.org/u/tommot1985)\
**Post date:** [December 13, 2018, 3:01pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/4 "2018-12-13T15:01:58Z")

</div>

Hi there you could implement this easily like so:

```
fun <S, T : S> List<T>.scan(op: (S, T) -> S): List<S> {
  return this.mapIndexed { i, _ ->
    this.subList(0, i + 1).reduce(op)
  }
}

```

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [December 13, 2018, 5:46pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/5 "2018-12-13T17:46:03Z")

</div>

@tommot1985 I wouldn’t recommend to use such implementation, as it has quadratic time complexity: for every element it needs to redo the operation for all elements before that one.

We’re considering to introduce functions like `scan` in this issue: [KT-7657](https://youtrack.jetbrains.com/issue/KT-7657).

---

<div class="post-metadata">

**Author:** ![tommot1985](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tommot1985/32/5785_2.png) [@tommot1985](https://discuss.kotlinlang.org/u/tommot1985)\
**Post date:** [December 13, 2018, 8:03pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/6 "2018-12-13T20:03:29Z")

</div>

I have thought up two versions with linear runtime (not checked):  
With mutable state:

```
fun <S, T : S> List<T>.scan(op: (S, T) -> S): List<S> {
   var accumulator: S = this[0]
   return this.mapIndexed { i, x ->
     if (i != 0) {
       accumulator = op(accumulator, x)
     }
     accumulator
   }
}

```

and without mutable state:

```
fun <S, T : S> List<T>.scan(op: (S, T) -> S): List<S> {
  fun tempFun(accumulator: S, list: List<T>, op: (S, T) -> S): List<S> {
    val result = op(accumulator, list.first())
    val cdr = list.drop(1)
    if (cdr.size > 0) {
      return listOf<S>(result) + tempFun(result, cdr, op)
    }
    return listOf<S>(result)
  }
  return listOf<S>(this[0]) + tempFun(this[0], this.drop(1), op)
}

```

---

<div class="post-metadata">

**Author:** ![kenbarc](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kenbarc/32/1693_2.png) [@kenbarc](https://discuss.kotlinlang.org/u/kenbarc)\
**Post date:** [December 14, 2018, 2:13pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/7 "2018-12-14T14:13:26Z")

</div>

I developed a Kotlin library of immutable datatypes.  
The **List** class includes the **scanLeft** member function which should meet your needs.  
The documentation taken from the function is:

/\*\*  
\* scanLeft is similar to foldLeft, but returns a list of successively  
\* reduced values from the left.  
\*  
\* Examples:  
\* [4, 2, 4].scanLeft(64){m, n → m / y} = [64, 16, 8, 2]  
\* .scanLeft(3){m, n → m / y} = [3]  
\* [1, 2, 3, 4].scanLeft(5){m, n → if (m \> n) m else n} = [5, 5, 5, 5, 5]  
\* [1, 2, 3, 4, 5, 6, 7].scanLeft(5){m, n → if (m \> n) m else n} = [5, 5, 5, 5, 5, 5, 6, 7]  
\*  
\* @param f uncurried binary function  
\* @param e initial value  
\* @return new list  
\*/

The library can be found [here](https://github.com/KenBarclay/SKData).

---

<div class="post-metadata">

**Author:** ![ezamelczyk](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@ezamelczyk](https://discuss.kotlinlang.org/u/ezamelczyk)\
**Post date:** [January 14, 2019, 4:47pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/8 "2019-01-14T16:47:05Z")

</div>

I might be wrong here, but the solution I’m using seems pretty trivial in implementation and with little preformance degradation:

```
fun <T, R: Any> List<T>.scan(op: (T, T) -> R): List<R> {
    return if(size > 1) {
        mapIndexedNotNull{ index, t ->
            if(index < size - 1) {
                op(get(index), get(index + 1))
            } else {
                null
            }
        }
    } else {
        emptyList()
    }
}

```

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 4, 2020, 5:17pm UTC](https://discuss.kotlinlang.org/t/does-kotlin-have-something-similar-to-rxjava-scan/275/9 "2020-03-04T17:17:02Z")

</div>

To follow up on this, it is now available in [Kotlin 1.3.70](https://blog.jetbrains.com/kotlin/2020/03/kotlin-1-3-70-released/)
