It - what does it mean?


What does the next line exactly mean: { square(it - d) }.average()


fun square(d: Double) = d * d

fun averageSquareDiff(d: Double, predictions: DoubleArray) = { square(it - d) }.average()

fun diversityTheorem(truth: Double, predictions: DoubleArray): String {
val average = predictions.average()
val f = “%6.3f”
return “average-error : ${f.format(averageSquareDiff(truth, predictions))}\n” +
“crowd-error : ${f.format(square(truth - average))}\n” +
“diversity : ${f.format(averageSquareDiff(average, predictions))}\n”

fun main(args: Array) {
println(diversityTheorem(49.0, doubleArrayOf(48.0, 47.0, 51.0)))
println(diversityTheorem(49.0, doubleArrayOf(48.0, 47.0, 51.0, 42.0)))

Gal Zsolt
(~ CalmoSoft ~)

Hello Johan,

Thanks for the help but I still do not know what it does mean.
How can I translate it to C language?
Please answer in details.

Gal Zsolt
(~ CalmoSoft ~)

There is a specific section about it in the article I linked to. You have to understand lambdas to understand what it says, but lambdas are the subject of the article. I don’t know what I can add that would improve the information in the article.

The more specific link:

It’s very common that a lambda expression has only one parameter.

If the compiler can figure the signature out itself, it is allowed not to declare the only parameter and omit ->. The parameter will be implicitly declared under the name it:

ints.filter { it > 0 } // this literal is of type '(it: Int) -> Boolean'

Hello Johan, Ilya,

Thanks for the help.
Now I understand what it does mean.

Gal Zsolt
(~ CalmoSoft ~)