Kotlin Sequence partition Function

The partition function in Kotlin is used to split a sequence into two lists based on a specified predicate. It is part of the Kotlin standard library and allows you to separate elements that satisfy the predicate from those that do not.

Table of Contents

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

Introduction

The partition function allows you to split a sequence into two lists based on a specified predicate. This is useful for scenarios where you need to categorize elements into two groups, enabling efficient data organization and analysis.

partition Function Syntax

The syntax for the partition function is as follows:

fun <T> Sequence<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>>

Parameters:

  • predicate: A lambda function that defines the condition each element in the sequence must satisfy.

Returns:

  • A pair of lists: the first list contains elements that satisfy the predicate, and the second list contains elements that do not.

Understanding partition

The partition function works by iterating through the sequence and applying the predicate to each element. Elements that satisfy the predicate are placed in the first list, while those that do not are placed in the second list. The function returns a pair of these lists.

Examples

Basic Usage

To demonstrate the basic usage of partition, we will create a sequence of integers and partition them into even and odd numbers.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5, 6)
    val (evenNumbers, oddNumbers) = numbers.partition { it % 2 == 0 }
    println("Even numbers: $evenNumbers") // Output: Even numbers: [2, 4, 6]
    println("Odd numbers: $oddNumbers") // Output: Odd numbers: [1, 3, 5]
}

Output:

Even numbers: [2, 4, 6]
Odd numbers: [1, 3, 5]

Partitioning a Sequence of Strings

This example shows how to partition a sequence of strings based on their length.

Example

fun main() {
    val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
    val (longNames, shortNames) = names.partition { it.length > 5 }
    println("Long names: $longNames") // Output: Long names: [Bhaskar, Chitra, Deepak]
    println("Short names: $shortNames") // Output: Short names: [Arjun, Esha]
}

Output:

Long names: [Bhaskar, Chitra, Deepak]
Short names: [Arjun, Esha]

Using partition with Custom Objects

You can use the partition function to split custom objects based on a specific condition.

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)
    )
    val (adults, minors) = people.partition { it.age >= 25 }
    println("Adults: $adults") // Output: Adults: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Deepak, age=28), Person(name=Esha, age=26)]
    println("Minors: $minors") // Output: Minors: [Person(name=Chitra, age=22)]
}

Output:

Adults: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Deepak, age=28), Person(name=Esha, age=26)]
Minors: [Person(name=Chitra, age=22)]

Chaining partition with Other Functions

The partition function can be chained with other sequence functions to perform more complex operations before partitioning the elements.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val (evenNumbers, oddNumbers) = numbers.filter { it > 4 }
                                           .partition { it % 2 == 0 }
    println("Even numbers greater than 4: $evenNumbers") // Output: Even numbers greater than 4: [6, 8, 10]
    println("Odd numbers greater than 4: $oddNumbers") // Output: Odd numbers greater than 4: [5, 7, 9]
}

Output:

Even numbers greater than 4: [6, 8, 10]
Odd numbers greater than 4: [5, 7, 9]

Real-World Use Case

Categorizing Products by Price

In real-world applications, the partition function can be used to categorize products or other items based on a specific property, such as price.

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),
        Product("Smartwatch", 199.99),
        Product("Headphones", 99.99)
    )

    val (expensiveProducts, cheapProducts) = products.partition { it.price > 300 }
    println("Expensive products: $expensiveProducts") // Output: Expensive products: [Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99)]
    println("Cheap products: $cheapProducts") // Output: Cheap products: [Product(name=Tablet, price=299.99), Product(name=Smartwatch, price=199.99), Product(name=Headphones, price=99.99)]
}

Output:

Expensive products: [Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99)]
Cheap products: [Product(name=Tablet, price=299.99), Product(name=Smartwatch, price=199.99), Product(name=Headphones, price=99.99)]

Conclusion

The partition function in Kotlin provides used for splitting elements of a sequence into two lists based on a specified predicate. By understanding and using the partition function, you can efficiently manage and process data in your Kotlin applications, ensuring that you organize collections of elements in a structured and meaningful way.

Leave a Comment

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

Scroll to Top