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
?