hho
02/09/2021, 7:32 PMval serials = devices.map { it.motor.serial }
2️⃣:
val serials = devices.map(Device::motor).map(Motor::serial)
Is there any reason ever to go with 2️⃣?Joel
02/09/2021, 7:33 PMval serials = devices.map { it.motor }.map { it.serial }
!Luke
02/09/2021, 7:41 PMSequence
, a list is instantiated at each .map
call. So 1️⃣ is better imoJoel
02/09/2021, 7:41 PMval totalMileage = devices.map { it.odometer }.sum() // O(n) memory
versus
val totalMileage = devices.fold(0) { acc, d -> acc + d.odometer } // O(1) memory
which is why .sumBy
exists.hho
02/10/2021, 1:21 AM