I am new to Scala, but I like to know what is the preferred way to solve this problem. Let's say I have a list of items, and I want to know the total amount of items that are checked. I could do something like this:
val total = items.filter(_.itemType == CHECK).map(._amount).sum
This will give me what I need, the sum of all checks in an immutable variable. But he does it with three iterations. To filter the checks, you again need to match the amounts, and then the amount. Another way is to do something like:
var total = new BigDecimal(0) for ( item <- items if item.itemType == CHECK ) total += item.amount
This gives me the same result, but with 1 iteration and a mutable variable, which seems fine too. But if I wanted to extract additional information, say that the total number of checks will require more counters or mutable variables, but I no longer have to iterate over the list. Not like a “functional” way to achieve what I need.
var numOfChecks = 0 var total = new BigDecimal(0) items.foreach { item => if (item.itemType == CHECK) { numOfChecks += 1 total += item.amount } }
So, if you need to have a bunch of counters or totals in the list, it’s preferable to save mutable variables or not to worry about it, something like:
val checks = items.filter(_.itemType == CHECK) val total = checks.map(_.amount).sum return (checks.size, total)
which seems more readable and uses only vals
source share