Vector map
sebasgarcep opened this issue · comments
Right now there is an apply
function for vectors which consumes it. The simplest method to simulate apply
without consuming the vector I can think of is to create an iterator, map over it and collect the result. I wonder if this is performant or if it would be better to add a map method to the vector struct?
The two best options I can think of (I hope this compiles, I haven't tried):
// What you suggested
let x = vector![1, 2, 3];
let mapped_vec = x.iter().map(|&x| f(&x)).collect::<Vector<_>>();
// A simple alternative?
let mapped_vec = x.clone().apply(|x| f(&x));
I would expect the first one to be perhaps slightly faster, but only measuring will tell you the truth.
In any case, I very much doubt that this is a bottleneck of your application. Unless you have measured this to be the case, I suggest you instead pick whichever one you find the most readable and carry on working on the interesting parts of your application ;-)
Thanks for the swift response!
I'm curious, why is there no apply
method which does not consume the vector?