Kotlin Koans 24 Max; min

Max; min (Playground)

Description

Implement Shop.getCustomerWithMaximumNumberOfOrders() and Customer.getMostExpensiveOrderedProduct() using max, min, maxBy, or minBy.

max, min, maxBy, もしくは minBy を使用して、 Shop.getCustomerWithMaximumNumberOfOrders() および Customer.getMostExpensiveOrderedProduct() を実装してください。

listOf(1, 42, 4).max() == 42
listOf("a", "ab").minBy { it.length } == "a"

Code

// Return a customer whose order count is the highest among all customers
fun Shop.getCustomerWithMaximumNumberOfOrders(): Customer? = customers.maxBy { it.orders.size }

// Return the most expensive product which has been ordered
fun Customer.getMostExpensiveOrderedProduct(): Product? = orders.flatMap { it.products }.maxBy { it.price }

Memo

← Blog