In Swift as per Apple's definition,
map
is used for returning an array containing the results of mapping the
given closure over the sequence’s elements
whereas,
forEach
calls the given closure on each element in the sequence in the same
order as a for-in loop.
Both got two different purposes in Swift. Even though in your example map works fine, it doesn't mean that you should be using map in this case.
map eg:-
let values = [1.0, 2.0, 3.0, 4.0]
let squares = values.map {$0 * $0}
[1.0, 4.0, 9.0, 16.0] //squares has this array now, use it somewhere
forEach eg:-
let values = [1.0, 2.0, 3.0, 4.0]
values.forEach() { print($0 * 2) }
prints below numbers. There are no arrays returned this time.
2.0
4.0
6.0
8.0
In short to answer your questions, yes the array generated from map is wasted and hence forEach is what you should use in this case.
Update:
OP has commented that when he tested, the performance was better for map compared to forEach. Here is what I tried in a playground and found. For me forEach performed better than map as shown in image. forEach took 51.31 seconds where as map took 51.59 seconds which is 0.28 seconds difference. I don't claim that forEach is better based on this, but both has similar performance attributes and which one to use, depends on the particular use case.

mapare discarded. Conversely, there is a strong argument for ensuring that allmapoperations are only for the result - and not used for any [write] side-effects. With that in mind, there is only one 'semantically correct' option regardless of any 'performance'. These simple Not Tricky Coding Choices (TM) are key to reducing frustration/wtf for those who work on the code after you.map, they are generating an array for a call. Then how does compiler optimizes? My understanding of_is that it just ignores the returned value.for elem in myArray.filter() { $0 != 42 } {...