In the article written by Daniel Korzekwa, he said that the performance of following code:
list.map(e => e*2).filter(e => e>10)
is muc
Rex Kerr correctly states the major problem: Operating on immutable lists, the stated piece of code creates intermediate lists in memory. Note that this is not necessarily slower than equivalent Java code; you just never use immutable datastructures in Java.
Wilfried Springer has a nice, Scala idomatic solution. Using view
, no (manipulated) copies of the whole list are created.
Note that using view might not always be ideal. For example, if your first call is filter
that is expected to throw away most of the list, is might be worthwhile to create the shorter version explicitly and use view
only after that in order to improve memory locality for later iterations.