Hullaballoonatic
12/01/2019, 1:35 AMfun Vector.times(other: Vector) = zip(other, Double::times).toVector()
or
fun Vector.times(other: Vector) = zip(other) { a, b -> a * b }.toVector()
?marcinmoskala
12/04/2019, 8:41 AMfun Vector.times(other: Vector) = Vector(this.x * other.x, this.y * other.y)
Is not only faster but also more readable.Hullaballoonatic
12/04/2019, 2:16 PMzip(other, Double::times).sum()
In general I don't like performing operations inside the arguments/parameters block unless they are very simple like in your example.Jannis
12/04/2019, 5:53 PMmarcinmoskala
12/04/2019, 6:27 PMHullaballoonatic
12/04/2019, 6:27 PMHullaballoonatic
12/04/2019, 6:28 PMHullaballoonatic
12/04/2019, 6:31 PMoperator fun Vector.times(other: Vector): Double {
if (size != other.size) error("Vector inner product requires vectors of equal length: $size != ${other.size}")
return zip(other, Double::times).sum()
}
Jannis
12/04/2019, 6:41 PMHullaballoonatic
12/04/2019, 7:02 PMx, y, ...
fields