Created
July 11, 2020 17:11
-
-
Save ericntd/892755307f8155264a3609cdbff162dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// prepare variable employees which is a list of Employee object | |
// Same as Task 0 | |
var groupCount = 0 | |
employees.groupingBy { it.department } | |
.aggregate { key, kAverage: Double?, element, isFirstElement -> | |
// Reset the group count | |
if (isFirstElement) { | |
groupCount = 1 | |
} else { | |
groupCount++ | |
} | |
return@aggregate if (isFirstElement) { | |
0.0 + element.salary | |
} else { | |
kAverage!! + (element.salary - kAverage)/(groupCount + 1) | |
} | |
} | |
.map { | |
println("Average salary for department ${it.key} is ${it.value}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment