Can we optimize these foreach loops?

The best way would be to use a cartesian function, but as far as I know the Kotlin standard library doesn’t have it out of the box. If it did you could write this example this way:

classA.itemsA.cartesian(classB.itemsB)
    .filter { (itemA, itemB) -> itemB.user == itemA.id }
    .forEach { classA.doSthA() }
2 Likes