DEV Community

Discussion on: Look ma', without loops!

Collapse
 
neonailol profile image
Valeriy Zhirnov • Edited

To avoid this you can use average function from stdlib, so istead of this:

fun calculateAverageScore(students: Array<Student>) = 
        students.sumBy { it.score } / students.size

You can write:

fun calculateAverageScore(students: Array<Student>) = 
        students.map { it.score }.average()