Nested lambda calls in Kotlin

When nesting lambda calls in Kotlin, how can I uniquely refer to the child and parent element it? 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} }
+4
source share
1 answer

italways refers to the innermost lambda parameter, in order to access the outer ones you have to name them. For instance:

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

(You can leave the inside like it, but it's a little better if you call it too, in my opinion.)

Edit: @ mfulton26 linked the related documentation below, see this for more details.

+13
source

Source: https://habr.com/ru/post/1673287/


All Articles