kotlinkotlin-extension

Swift enumerated equivalent in Kotlin


In a 3x3 matrix representation, i can find the sum of both diagonals with one liners in Swift as below,

let array = [
   [1, 2, 3],
   [4, 5, 6],
   [-7, 8, 9]
]
let d1 = array.enumerated().map({ $1[$0] }).reduce(0, +)
let d2 = array.reversed().enumerated().map({ $1[$0] }).reduce(0, +)
print(d1) // prints 15
print(d2) // prints 1

I am able to find map and reduce equivalents in Kotlin as flatMap and fold but couldn't find for enumerated.

How can we achieve similar with higher order functions in Kotlin?


Solution

  • Starting with this input:

    val input: Array<Array<Int>> = arrayOf(
            arrayOf(1, 2, 3),
            arrayOf(4, 5, 6),
            arrayOf(-7, 8, 9)
    )
    

    this is how I'd phrase the diagonal sums:

    val mainDiagonalSum = input.indices
            .map { input[it][it] }
            .reduce(Int::plus)
    val counterDiagonalSum = input.indices
            .map { input[input.size - 1 - it][it] }
            .reduce(Int::plus)
    

    Note that this is an improvement over your solution because it doesn't have to create the reversed array. It improves the time complexity from O(n2) to O(n).

    If you're dealing with large matrices, it would pay to reduce the space complexity from O(n) to O(1) as well, by using fold instead of reduce:

    val mainDiagonalSum = input.indices
            .fold(0) { sum, i -> sum + input[i][i] }
    val counterDiagonalSum = input.indices
            .fold(0) { sum, i -> sum + input[input.size - 1 - i][i] }