Showing posts with label Partition function. Show all posts
Showing posts with label Partition function. Show all posts

Sunday, 18 April 2021

Kotlin - Partition Function

Splits the original array / collections into pair of lists, where first list contains elements for which predicate yielded true, while second list contains elements for which predicate yielded false.

Please refer the below examples, here we used 2 filters to get the objects with certain crieteria, but it can be easily achieved through partition function, this executes the predicate and produce the pairs of list and we can access directly on pari's (first and second function) or we can destructure and use the same. 

data class Movies(val id: String,
                      val movieName: String,
                      val rating: Double = 1.0)

    val movies = listOf(Movies("100XA", "Guru", 4.5),
            Movies("100JK", "Ghab", 3.2),
            Movies("100HN", "Qualis", 1.2),
            Movies("1089O", "Tree"))

    // Using Filter
    println("### Rating Above 3 - Filter ###")
    movies.filter { it.rating > 3 }.forEach { println(it.movieName) }

    println("### Rating Not Above 3 - Filter ###")
    movies.filterNot { it.rating > 3 }.forEach { println(it.movieName) }

    // Using Partition - Destructuring the paris of list
    val (above3, notAbove3) = movies.partition { it.rating > 3 }
    println("\n### Rating Above 3 - Partition  ###")
    above3.forEach { println(it.movieName) }

    println("### Rating Not Above 3 - Partition  ###")
    notAbove3.forEach { println(it.movieName) }

Output:

### Rating Above 3 - Filter ###
Guru
Ghab
### Rating Not Above 3 - Filter ###
Qualis
Tree

### Rating Above 3 - Partition  ###
Guru
Ghab
### Rating Not Above 3 - Partition  ###
Qualis
Tree

Sunday, 6 December 2020

Kotlin - Collection Partition function

Splits the original array / collection into pair of lists, where first list contains elements for which predicate yielded true, while second list contains elements for which predicate yielded false.  
Please refer the below Code Snippet for the details.

val employeeList = listOf(
EmployeeInfo("HB100", "Ragavan A", 30),
EmployeeInfo("AX782", "Peter J", 54),
EmployeeInfo("ZD242", "Karlos V", 56),
EmployeeInfo("CD902", "Ismail N", 24)
)

val (ageAbove50, ageBelow50) = employeeList.partition { it.eAge > 50 }
println("\nEmployee List: $employeeList")
println("\nAge Above 50: $ageAbove50")
println("\nAge Below 50: $ageBelow50")

/*Since it returns as pairs, If we want to ignore the other condition i.e. 
ageBelow50 employees use _*/
val (ageAbove50Plus, _) = employeeList.partition { it.eAge > 50 }
println("\nAge 50+: $ageAbove50Plus")

Output:

Employee List: 
[EmployeeInfo(eId=HB100, eName=Ragavan A, eAge=30), 
EmployeeInfo(eId=AX782, eName=Peter J, eAge=54), 
EmployeeInfo(eId=ZD242, eName=Karlos V, eAge=56), 
EmployeeInfo(eId=CD902, eName=Ismail N, eAge=24)]

Age Above 50: [EmployeeInfo(eId=AX782, eName=Peter J, eAge=54), 
EmployeeInfo(eId=ZD242, eName=Karlos V, eAge=56)]

Age Below 50: [EmployeeInfo(eId=HB100, eName=Ragavan A, eAge=30), 
EmployeeInfo(eId=CD902, eName=Ismail N, eAge=24)]

Age 50+: [EmployeeInfo(eId=AX782, eName=Peter J, eAge=54), 
EmployeeInfo(eId=ZD242, eName=Karlos V, eAge=56)]