I wrote the following in Swift to perform a sum:
let total:Double = session.courses.reduce(0) { 0ドル + 1ドル.totalPrice() }
It's concise, it does the job but it does not seems very readable to me. Wouldn't a "classic" for
loop be more explicit/readable/maintainable?
var total:Double = 0
for dish in session.courses {
total += dish.totalPrice()
}
Should closures always be the way to go in Swift?
2 Answers 2
Both solutions are correct. Some advantages of the "functional" approach
with reduce()
:
- Concise code.
- Returns a constant. It is clear to the reader that
total
is assigned to exactly once, and the compiler may use this information for optimizing code. The type annotation on the result value is not needed:
let total = session.courses.reduce(0) { 0ドル + 1ドル.totalPrice() }
because that can be inferred from the compiler automatically. This will continue to compile without changes if the type of
totalPrice()
is changed, e.g. fromDouble
toFloat
.
To increase legibility, you can use named closure parameters instead
of the $n
shortcuts:
let total = session.courses.reduce(0) { (partialSum, dish) in
partialSum + dish.totalPrice()
}
-
1\$\begingroup\$ +1 for legibility! I find the
$n
shortcuts incredibly hard to read, and they offer no information on what the values represent. \$\endgroup\$user1118321– user11183212018年01月17日 03:18:02 +00:00Commented Jan 17, 2018 at 3:18
Another alternatives:
let sum = session.courses.map({0ドル.totalPrice()}).reduce(0, +)
Another note, I think it's more elegant to implement totalPrice
as computed variable instead of function.
Another way is to use forEach
:
var sum = 0
session.courses.forEach {sum+=0ドル.totalPrice()}
as @Martin R mentioned the functional approach has some advantages especially that the result is constant.