When nesting lambda calls in Kotlin, how can I unambiguously refer to child's and parent's it element? For instance:

data class Course(var weekday: Int, var time: Int, var duration: Int)
var list1 = mutableListOf<Course>()
var list2 = mutableListOf<Course>()
// populate list1 and list2
// exclude any element from list1 if it has the same time and weekday as any element from list2
list1.filter { list2.none{it.weekday == it.weekday && it.time == it.time} }

推荐答案

it always refers to the innermost lambda's parameter, to access outer ones, you have to name them. For example:

list1.filter { a -> list2.none { b -> a.weekday == b.weekday && a.time == b.time} }

(You could leave the inner one as it, but it's a bit nicer if you name that too, in my opinion.)

Edit: @mfulton26 linked the relevant documentation below, see that for more details.

Kotlin相关问答推荐

在Kotlin中处理结果的高阶函数

我可以检测一个函数是否在Kotlin中被递归调用(即,重入)吗?

使用另一个对象的列表创建对象

在调用父构造函数之前重写类属性

jOOQ Kotlin Coroutines - Select all and exists查询

如何在不基于数据 map 的情况下将图例添加到lets plot kotlin

两个LocalDateTime之间的Kotlin差异

有什么方法可以要求在 kotlin 中的类型参数上进行注释?

每个 Kotlin 版本的默认 Kotlin 语言版本是什么?

kotlin 单例异常是好是坏?

Jetpack BottomNavigation - java.lang.IllegalStateException:Already attached to lifecycleOwner

Kotlin 中列表或数组的乘积

Android 在将 androidx 生物识别更新为 1.0.0-alpha04 后崩溃

Kotlin 1.2.21 + SimpleXml 2.3.0 - consume List error (must mark set get method)

如何从 Java 中隐藏 Kotlin 的 lateinit var 支持字段?

如何在主线程上使用 Kotlin 协程 await()

Kotlin 类的调用方法

Kotlin 的类型具体化使哪些在 Java 或 Scala 中无法实现的成为可能?

kotlin RecyclerView分页

Kotlin协程处理错误和实现