가 통화별로 합계를 계산한다는 결론에 이른다. 일단 모든 데이터를 모으고 나면, 지금 우리 코
드가 하는 것처럼 통화별로 그룹을 나누고 각 그룹에 속한 리스트의 합계를 구함으로써 똑같은
계산을 수행할 수 있다.
예제
14.27
[
accumulator
.
14
:
src
/
main
/
java
/
travelator
/
itinerary
/
CostSummaryCalculator
.
kt
]
class CostSummaryCalculator(
private val userCurrency: Currency,
private val exchangeRates: ExchangeRates
) {
fun summarise(costs: Iterable<Money>): CostSummary {
val currencyTotals: List<Money> = costs
.groupBy { it.currency }
.values
.map { moneys -> moneys.reduce(Money::add) }
val lines: List<CurrencyConversion> = currencyTotals ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.