Skip to content

Instantly share code, notes, and snippets.

@anoopmaddasseri
Created October 4, 2020 06:25
Show Gist options
  • Save anoopmaddasseri/444d46cb14a1e3acd0f2f1691a6dda16 to your computer and use it in GitHub Desktop.
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
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