kotlin - 哪种是Kotlin koan “Partition”的首选解决方案,为什么?

标签 kotlin partition

我研究了Kotlin koan分区,并通过了单元测试:

    fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = 
        customers.filter{it.orders.partition{it.isDelivered}
             .let{(delivered, undelivered) -> delivered.size < undelivered.size}}.toSet()

这是解决方案的一种变体:
    fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = 
        customers.filter {
            val (delivered, undelivered) = it.orders.partition { it.isDelivered }
            undelivered.size > delivered.size
        }.toSet()

是否有任何理由偏爱其中一种解决方案?

最佳答案

Kotlin Koans是为Kotlin 1.0编写的,它不支持lambda参数的解构。

否则,如果您以Kotlin 1.1为目标,那么这两个解决方案是等效的。

关于kotlin - 哪种是Kotlin koan “Partition”的首选解决方案,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44447212/

相关文章:

Android Kotlin Realm 查询的正确方法+ 在 Bg 线程上返回非托管项目

kotlin - 构造函数可见性仅限于文件

java - 集合的分区 - 将结果存储在一系列嵌套列表中

java - Iterables.partition() 生成的 Spliterator 没有按预期运行?

python - 为什么 Python 的 str.partition 返回分隔符?

database - AWS Athena 的分区数据导致 S3 中有很多小文件

hibernate - 我应该使用 Kotlin 数据类作为 JPA 实体吗?

android - 退格键仅删除 TextField 中的一个字母

相同类的 Kotlin 密封类

linux - 如果我使用ubuntu + Windows,如何从未分配的分区增加ubuntu中主目录的磁盘大小?