I have a Kotlin Vector class that includes a function to calculate the dot product of two Vectors:
class Vector(val values: Array<Double>) {
fun dot(v: Vector): Double {
require(this.values.size == v.values.size)
var product = 0.0
for (i in this.values.indices) {
product += this.values[i] * v.values[i]
}
return product
}
}
I'd like to express the dot product of two vectors in a functional style. Fold would initialize, but I don't see how to make it work with two arrays.
Does anyone have a suggestion?