enum class EducationLevel { HIGH_SCHOOL, BACHELORS, MASTERS, PHD } data class Person(val age: Int, val smoker: Boolean, val income: Int, val education: EducationLevel) val people = listOf( Person(19, false, 10000, EducationLevel.HIGH_SCHOOL), Person(49, true, 120000, EducationLevel.BACHELORS), Person(55, false, 400000, EducationLevel.MASTERS), Person(23, true, 10000, EducationLevel.BACHELORS), Person(70, false, 70000, EducationLevel.PHD), Person(34, false, 90000, EducationLevel.MASTERS), Person(90, true, 0, EducationLevel.HIGH_SCHOOL) ) fun income_by_smoking(people: List) = people .exclude_incomes_under(10000). separate_people_by { it.smoker }. average_income() fun List.exclude_incomes_under(amount: Int): List = this.filter { it.income > amount } fun List.separate_people_by(selector: (Person) -> K): Map> = this.groupBy { selector(it) } fun Map>.average_income(): Map = this.mapValues { it.value.sumBy { it.income } } fun main(args: Array) { val message = """ Result: ${income_by_smoking(people)} """ println(message) }