Question regarding documentation

Hi, I was reading the documentation for fold function and a question arose.

the acc below which is nested in operation:

inline fun <T, R> Array<out T>.fold(
    initial: R,
    operation: (acc: R, T) -> R
): R

or key, accummulator and element below which is is also nested

inline fun <T, K, R> Grouping<T, K>.fold(
    initialValueSelector: (key: K, element: T) -> R,
    operation: (key: K, accumulator: R, element: T) -> R
): Map<K, R>

How should I interpret these?
How is this used?
Does this mean that inside function fold, its parameter operation, which is a function, should have those exact parameter names, not only following the parameter types?

Hi,

The parameter names of the operation function are only hints to help discovery of method usage, but there’s no constraint on them. It only serves as indication about parameter purpose. If we take array fold as example, we can make th following code:

​
fun main() {
    val txt = arrayOf("first", "second").fold("", ::concat)
    println("txt")
}
​
fun concat(prefix : String, suffix : String) = "$prefix$suffix"

As you see, my concat function parameter names does not use acc, but it works as I give a function with the right number/type of arguments.

Does it make things clear ?

1 Like

Thanks, that cleared things up :smiley: So it’s just for helping readability