Created
October 4, 2020 06:25
-
-
Save anoopmaddasseri/444d46cb14a1e3acd0f2f1691a6dda16 to your computer and use it in GitHub Desktop.
Kotlin: Find sum of all values produced by the selector function applied to each element in the collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val sumByDouble = | |
data.sumByDouble { serviceGroup: PackageDetails.ServiceGroup -> serviceGroup.serviceItems.price } | |
++++++++++++++++++++++++++++++++++++++++++ | |
val nums = listOf(10, 20, 30) | |
println(nums.sum()) | |
val totalQuantity: Int = products.map { it.quantity }.sum() | |
++++++++++++++++++++++++++++++++++++++++++ | |
val nums = listOf(10, 20, 30) | |
println(nums.sumBy { it }) | |
println(nums.sumBy { it * 2 }) | |
val doubles = listOf(1.05, 2.05, 3.65) | |
println(doubles.sumBy { it.roundToInt() }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment