Just thought I’d share something that I was just caught out by, I was trying to combine a series of functions on some data and used a non-commutative function which completely changed the meaning in a way that took me some time to track down. I was doing the following:
let s = Vec2d.create x y |> Vec2d.sub offset |> Vec2d.scale d |> Vec2d.add subsample
Here I’ve got a 2d vector from which I want to subtract another vector, scale it by a scalar value and add another vector back on to. This mostly works but since
Vec2d.sub is non-commutative what it actually does is subtract the created vector from the offset. Since the dominating value is actually the
y values not the offset this had the most notable effect of inverting the axis!
Anyway thought I’d share to alert any other learners that they should be careful when using this operator with non-commutative functions.
Also if anyone has a good suggestion for general workarounds to this, I would love to hear them. What I’ve done is create a
sub_by function which doesn’t feel very satisfactory and adds an unnecessary function to my Vec2d module.