I often have to summarize the conversion of a list of numbers to Scala. Of course, one way to do this is:
list.map(transform(_)).sum
However, this creates memory when the creation of memory is not required. An alternative is to collapse the list.
list.foldLeft(0.0) { (total, x) => total + f(x) }
I find that the first expression is much easier to write than the second expression. Is there a way that I can use that has the ease of the first with the effectiveness of the second? Or am I better off writing my own implicit method?
list.mapSum(transform(_))
schmmd
source share