Fold left/right signature


#1

Hello,

According the API doc fold is following:  fun <T> fold(initial: T, operation: (T, T) -> T): T

In my mind such signature is quite restrictive. For example, if I have an array/list of characters (word) and I want to calculate characters occurrence in the word, in kotlin I cannot accomplish such task using fold, in scala I can.

Scala fold signature is kind of def foldLeft[B](z : B)(f : scala.Function2[B, A, B]) : B

And to get characters occurrence (as map) I could write: chars.foldLeft(Map())((m, ch) => m.updated(ch, m.getOrElse(ch, {0}) + 1))

Wouldn’t it be better (if possible) to make fold signature similar to Scala one?

Regards,
Vadim


#2

Unfortunately API doc is a bit out of date. (KT-2914) And now fold has following signature:

fun <T, R> fold(initial: R, operation: (R, T) -> R): R

(related issue KT-2091)


#3

It seems that koltin web demo is also a little bit out of date - fold signature is the same as in out-dated API docs. Since what milestone/version has fold method signature been changed?

Vadim


#4

It works since milestone 3.1(v.0.4.68) Yes, compiler on webdemo is a bit out of date too. Can you create issue about it? Thanks. It will be fixed soon.


#5

Done: KT-3022

Vadim