``` val Array<Coordinate>.centroid get()...
# codereview
b
Copy code
val Array<Coordinate>.centroid
    get() = fold(mutableMapOf<Double, Double>()) { acc, coord -> acc.also { it[coord.x] = coord.y } }.run {
        Coordinate(keys.average(), values.average())
    }
Is there a better way to get the average coordinate from an array of coordinates? (Coordinate is a simple data class holding two doubles, x and y)
d
To just simplify....
Copy code
val Array<Coordinate>.centroid
    get() = associateBy({ it.x }, { it.y }).run {
        Coordinate(keys.average(), values.average())
    }
b
oh lol
thanks
d
Better would be something like this.
Copy code
val Array<Coordinate>.centroid
    get() = Coordinate(map { it.x }.average(), map { it.y }.average())
Sadly there's no
averageBy
.
3
b
oh that's also good
yeah I'm not sure how I avoided any of the simpler options but thanks again