Kotlin Sequence average Function

The average function in Kotlin is used to calculate the average of all elements in a sequence of numeric values. It is part of the Kotlin standard library and allows you to compute the mean of the elements in a sequence easily.

Table of Contents

  1. Introduction
  2. average Function Syntax
  3. Understanding average
  4. Examples
    • Basic Usage
    • Calculating Average of a Sequence of Doubles
    • Using average with Custom Objects
    • Chaining average with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The average function allows you to calculate the mean of all elements in a sequence of numeric values. This is useful for scenarios where you need to determine the average value of a collection of numbers, enabling efficient data analysis and summary statistics.

average Function Syntax

The syntax for the average function is as follows:

fun Sequence<Int>.average(): Double
fun Sequence<Long>.average(): Double
fun Sequence<Float>.average(): Double
fun Sequence<Double>.average(): Double

Returns:

  • The average of the elements in the sequence as a Double.

Understanding average

The average function works by iterating through the sequence, summing all the elements, and then dividing the sum by the number of elements. If the sequence is empty, the function returns NaN (Not-a-Number).

Examples

Basic Usage

To demonstrate the basic usage of average, we will create a sequence of integers and calculate their average.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5)
    val avg = numbers.average()
    println(avg) // Output: 3.0
}

Output:

3.0

Calculating Average of a Sequence of Doubles

This example shows how to calculate the average of a sequence of doubles.

Example

fun main() {
    val numbers = sequenceOf(1.5, 2.5, 3.5, 4.5, 5.5)
    val avg = numbers.average()
    println(avg) // Output: 3.5
}

Output:

3.5

Using average with Custom Objects

You can use the average function to calculate the average of a specific property of custom objects by mapping the sequence to the property values.

Example

data class Product(val name: String, val price: Double)

fun main() {
    val products = sequenceOf(
        Product("Laptop", 999.99),
        Product("Smartphone", 499.99),
        Product("Tablet", 299.99)
    )
    val averagePrice = products.map { it.price }.average()
    println(averagePrice) // Output: 599.99
}

Output:

599.99

Chaining average with Other Functions

The average function can be chained with other sequence functions to perform more complex operations before calculating the average.

Example

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

Output:

6.0

Real-World Use Case

Calculating Average Rating

In real-world applications, the average function can be used to calculate the average rating from a sequence of ratings.

Example

data class Review(val product: String, val rating: Double)

fun main() {
    val reviews = sequenceOf(
        Review("Laptop", 4.5),
        Review("Smartphone", 4.0),
        Review("Tablet", 3.5),
        Review("Headphones", 4.0)
    )

    val averageRating = reviews.map { it.rating }.average()
    println(averageRating) // Output: 4.0
}

Output:

4.0

Conclusion

The average function in Kotlin provides used for calculating the average of all elements in a sequence of numeric values. By understanding and using the average function, you can efficiently manage and process data in your Kotlin applications, ensuring that you can compute the mean of collections of numbers according to your requirements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top