Kotlin Koans 28 Partition

目次

Partition (Playground)

Description

Implement Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered() using partition.

partition を読んで、Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered() を実装してください。

val numbers = listOf(1, 3, -4, 2, -11)
val (positive, negative) = numbers.partition { it > 0 }
positive == listOf(1, 3, 2)
negative == listOf(-4, -11)

Note that destructuring declaration syntax is used in this example.

この例では 破壊宣言 の構文に注意してください。

Code

// Return customers who have more undelivered orders than delivered
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = customers.filter {
    val (delivered, undelivered) = it.orders.partition { it.isDelivered }
    undelivered.size > delivered.size
}.toSet()

Memo

破壊宣言については Destructuring declarations でおさらい

← Posts