Kotlin Sequence sumBy Function

The sumBy function in Kotlin is used to calculate the sum of values produced by applying a selector function to each element in a sequence. It is part of the Kotlin standard library and allows you to sum the values of a specific property of elements in a sequence.

Table of Contents

  1. Introduction
  2. sumBy Function Syntax
  3. Understanding sumBy
  4. Examples
    • Basic Usage
    • Summing Values in a Sequence of Strings
    • Using sumBy with Custom Objects
    • Chaining sumBy with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The sumBy function allows you to calculate the sum of values produced by a selector function applied to each element in a sequence. This is useful for scenarios where you need to compute the total of a specific property of elements, such as summing prices, lengths, or other numerical attributes.

sumBy Function Syntax

The syntax for the sumBy function is as follows:

inline fun <T> Sequence<T>.sumBy(selector: (T) -> Int): Int 

Parameters:

  • selector: A lambda function that defines how each element in the sequence should be transformed into an integer value to be summed.

Returns:

  • The sum of the values produced by the selector function.

Understanding sumBy

The sumBy function works by iterating through the sequence and applying the selector function to each element to produce an integer value. These values are then summed together to produce the final result.

Examples

Basic Usage

To demonstrate the basic usage of sumBy, we will create a sequence of integers and compute the sum of their squares.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5) val sumOfSquares = numbers.sumBy { it * it } println(sumOfSquares) // Output: 55 } 

Output:

55 

Summing Values in a Sequence of Strings

This example shows how to sum the lengths of strings in a sequence.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val totalLength = names.sumBy { it.length } println(totalLength) // Output: 29 } 

Output:

29 

Using sumBy with Custom Objects

You can use the sumBy function to sum properties of custom objects.

Example

data class Product(val name: String, val price: Int) fun main() { val products = sequenceOf( Product("Laptop", 999), Product("Smartphone", 499), Product("Tablet", 299) ) val totalPrice = products.sumBy { it.price } println(totalPrice) // Output: 1797 } 

Output:

1797 

Chaining sumBy with Other Functions

The sumBy function can be chained with other sequence functions to perform more complex operations before summing the values.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val evenSumOfSquares = numbers.filter { it % 2 == 0 } .sumBy { it * it } println(evenSumOfSquares) // Output: 220 } 

Output:

220 

Real-World Use Case

Calculating Total Revenue

In real-world applications, the sumBy function can be used to calculate the total revenue from a sequence of sales.

Example

data class Sale(val product: String, val revenue: Int) fun main() { val sales = sequenceOf( Sale("Laptop", 1000), Sale("Smartphone", 600), Sale("Tablet", 300), Sale("Headphones", 150) ) val totalRevenue = sales.sumBy { it.revenue } println(totalRevenue) // Output: 2050 } 

Output:

2050 

Conclusion

The sumBy function in Kotlin provides used for calculating the sum of values produced by a selector function applied to each element in a sequence. By understanding and using the sumBy function, you can efficiently manage and process data in your Kotlin applications, ensuring that you can compute totals of specific properties according to your requirements.

Leave a Comment

Scroll to Top