Aesthetically and mathematically a map(f, X) function seems great: in just two arguments it tells what is going to happen: we are going to use function f to transform each element from X to create a new array Y.However I do not know what is happening under the bonnet of either for or map; and my reasons for liking map could be argued to be superficial. Is one any more efficient than the other?Note: the question is not about micro-optimisation: the question is a broad (multi-criterion) comparison between two specific methods (that seem to achieve the same objective) and why would one use one or another.
Transforming one array to another: a for loop or a functional map?
functional programming
97% of the time you should use the one that is easier to understand and therefore easier to get correct. I'd also note that there is also an implicit immutable vs mutable data issue here. map is always immutable and will always return a new collection of things, for may or may not be used immutably.I'd lean toward immutability as easier to get correct moving to a mutable solution only if it was required. I'd therefore lean towards using map as it better expresses this intent.