Nikolay Puliaev
04/22/2021, 11:34 AMval red = data.red.map { it.toUByte().toInt() }.average()
val green = data.green.map { it.toUByte().toInt() }.average()
val blue = data.blue.map { it.toUByte().toInt() }.average()
val luminance = red + green + blueJonathan Olsson
04/22/2021, 11:49 AMval lumincance = listOf(data.red, data.green, data.blue).sumOf { cs ->
cs.map {
it.toUByte().toInt()
}.average()
}Michael Böiers
04/22/2021, 11:51 AMval luminance = with(data) {
sequenceOf(red, green, blue)
.map { it.map(SomeClass::toUByte).map(UByte::toInt)}
.map { it.average() }
.sum()
}Michael Böiers
04/22/2021, 11:52 AMNikolay Puliaev
04/22/2021, 11:54 AMNir
04/22/2021, 1:57 PMNikolay Puliaev
04/22/2021, 1:58 PMNir
04/22/2021, 1:58 PMcs.asSequence().mapNir
04/22/2021, 1:59 PMNikolay Puliaev
04/22/2021, 1:59 PMNir
04/22/2021, 1:59 PMMichael Böiers
04/22/2021, 2:02 PMMichael Böiers
04/22/2021, 2:03 PMMichael Böiers
04/22/2021, 2:03 PMMichael Böiers
04/22/2021, 2:04 PMlistOf("a", "xx").averageOf { it.length }Michael Böiers
04/22/2021, 2:05 PMNir
04/22/2021, 2:08 PMNir
04/22/2021, 2:08 PMNir
04/22/2021, 2:09 PMMichael Böiers
04/22/2021, 2:15 PMinline fun <T> kotlin.collections.Iterable<T>.averageOf(selector: (T) -> <http://kotlin.Int|kotlin.Int>): kotlin.Double {
var sum: Long = 0.toLong()
var count = 0
for (element in this) {
sum += selector(element)
count++
}
return sum.toDouble()/count
}
listOf("a", "xx").averageOf { it.length }Nir
04/22/2021, 2:33 PMNir
04/22/2021, 2:34 PMNir
04/22/2021, 2:34 PMNir
04/22/2021, 2:34 PMMichael Böiers
04/22/2021, 2:34 PMMichael Böiers
04/22/2021, 2:35 PMNir
04/22/2021, 2:36 PMMichael Böiers
04/22/2021, 2:37 PMNir
04/22/2021, 2:37 PMNir
04/22/2021, 2:38 PMNir
04/22/2021, 2:38 PMMichael Böiers
04/22/2021, 2:39 PMNir
04/22/2021, 2:39 PMNir
04/22/2021, 2:41 PMJonathan Olsson
04/22/2021, 4:15 PMNir
04/22/2021, 4:16 PMJonathan Olsson
04/22/2021, 4:19 PMNir
04/22/2021, 4:23 PMNir
04/22/2021, 4:23 PMNir
04/22/2021, 4:24 PMJonathan Olsson
04/22/2021, 7:07 PMsum overflow way before count?sum is Double in current implementationJonathan Olsson
04/22/2021, 7:16 PMJonathan Olsson
04/22/2021, 7:16 PMMichael Böiers
04/22/2021, 9:32 PMNir
04/22/2021, 9:45 PMNir
04/22/2021, 9:46 PMNir
04/22/2021, 9:47 PMMichael Böiers
04/23/2021, 6:37 AMaverage() to do the naive computation that everybody knows, which is summing all the values and dividing by the count. Then we could have other functions that are named by algorithm, so that clients know what they can expect.”Jonathan Olsson
04/23/2021, 7:08 AMJonathan Olsson
04/23/2021, 7:09 AMJonathan Olsson
04/23/2021, 7:09 AMNir
04/23/2021, 1:50 PMaverage should be a "reasonable" implementation of computing the average. That is, it should do a pretty good job, and perform pretty well. Looking at the prior art, you can hugely improve results at minor performance cost. So I don't see an intrinsic argument for the naive approach.Nir
04/23/2021, 1:50 PMsort should be bubble sort, or insertion sort. These are after all the simplest algorithms.Nir
04/23/2021, 1:51 PMNir
04/23/2021, 1:52 PM