Kotlin Sequence forEach Function

The forEach function in Kotlin is used to perform an action on each element of a sequence. It is part of the Kotlin standard library and provides a way to iterate through a sequence and apply a specified action to each element.

Table of Contents

  1. Introduction
  2. forEach Function Syntax
  3. Understanding forEach
  4. Examples
    • Basic Usage
    • Using forEach with Strings
    • Using forEach with Custom Objects
    • Chaining forEach with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The forEach function allows you to iterate through a sequence and perform an action on each element. This is useful for scenarios where you need to apply the same operation to every element in the sequence, such as printing each element or updating values.

forEach Function Syntax

The syntax for the forEach function is as follows:

inline fun <T> Sequence<T>.forEach(action: (T) -> Unit) 

Parameters:

  • action: A lambda function that defines the action to perform on each element.

Returns:

  • The function does not return a value.

Understanding forEach

The forEach function works by iterating through the sequence and applying the specified action to each element. The action is defined as a lambda function that takes an element of the sequence as its argument.

Examples

Basic Usage

To demonstrate the basic usage of forEach, we will create a sequence of integers and print each element.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5) numbers.forEach { println(it) } } 

Output:

1 2 3 4 5 

Using forEach with Strings

This example shows how to use forEach to iterate through a sequence of strings and print each element.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") names.forEach { println(it) } } 

Output:

Arjun Bhaskar Chitra Deepak Esha 

Using forEach with Custom Objects

You can use the forEach function to iterate through a sequence of custom objects and perform an action on each object.

Example

data class Person(val name: String, val age: Int) fun main() { val people = sequenceOf( Person("Arjun", 25), Person("Bhaskar", 30), Person("Chitra", 22), Person("Deepak", 28), Person("Esha", 26) ) people.forEach { println("${it.name} is ${it.age} years old") } } 

Output:

Arjun is 25 years old Bhaskar is 30 years old Chitra is 22 years old Deepak is 28 years old Esha is 26 years old 

Chaining forEach with Other Functions

The forEach function can be chained with other sequence functions to perform more complex operations before applying the action to each element.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) numbers.filter { it % 2 == 0 } .forEach { println(it) } } 

Output:

2 4 6 8 10 

Real-World Use Case

Updating a List of Products

In real-world applications, the forEach function can be used to iterate through a sequence of products and update their prices.

Example

data class Product(val name: String, var price: Double) fun main() { val products = sequenceOf( Product("Laptop", 1000.0), Product("Smartphone", 600.0), Product("Tablet", 300.0), Product("Headphones", 150.0) ) products.forEach { it.price *= 0.9 } // Applying a 10% discount to each product products.forEach { println("${it.name}: ${it.price}") } // Output: // Laptop: 900.0 // Smartphone: 540.0 // Tablet: 270.0 // Headphones: 135.0 } 

Output:

Laptop: 900.0 Smartphone: 540.0 Tablet: 270.0 Headphones: 135.0 

Conclusion

The forEach function in Kotlin provides used for iterating through a sequence and performing actions on each element. By understanding and using the forEach function, you can efficiently manage and process data in your Kotlin applications, ensuring that operations are applied consistently to all elements in a sequence.

Leave a Comment

Scroll to Top