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相关问答推荐

如何在Jetpack Compose中的列中渲染图像

在KMP中使用koin将来自Android的上下文注入到SQLDelight Driver中

在Kotlin 有更好的结合方式?

Kotlin编译器如何决定是否可以在任何给定点调用Suspend方法?

访问者闭包中的Kotlin序列yield 率

禁用 Gradle执行消息

在 Kotlin 中将 Array 转换为 IntArray 时丢失值

Kotlin 获取继承类的默认 hashCode 实现

如何使用 Mockk 模拟返回值类的 Kotlin 函数类型?

内容更改后的 var 重新计算

如何将glide显示的图像下载到设备存储中.Kotlin

使用 LazyListScope 嵌套可组合项

compareBy 如何使用布尔表达式在 kotlin 中工作

在粘贴时将 java 转换为 kotlin

Kotlin默认使用哪种排序?

如何在Spring Boot应用程序上启用承载身份验证?

Kotlin惰性默认属性

不推荐使用仅限生命周期的LifecycleEvent

IllegalStateException:function = , count = 3, index = 3

Kotlin var lazy init