Showing posts with label filter. Show all posts
Showing posts with label filter. Show all posts

Sunday, 19 December 2021

Passing function as a param to collection filter

It's actually simpler to provide a lambda directly, There's other option that's worth covering, too, which is a function / lambda reference.  If you already had a function that would do the job, you don't need to put it in a lambda, but can refer to it directly, through function or its reference using the :: notation. this will works only if the parameter type(s) are compatible, but it's slightly simpler and can generate slightly more efficient bytecode.


val myValues = listOf(12, 25, 215, 3, 52)
println("All Values: $myValues")
println("Even Filter ${myValues.filter { it % 2 == 0 }} -- Simple implicit lambda")
println("Odd Filter ${myValues.filter { oddFilter(it) }} -- lambda reference as param")
println("odd Filter ${myValues.filter(oddFilter)} -- lambda as param to filter function")
println("Even Filter ${myValues.filter(::evenFilter)} -- function reference as param")

val oddFilter = { a: Int -> a % 2 != 0 }

fun evenFilter(g: Int): Boolean = g % 2 == 0

Output:

All Values: [12, 25, 215, 3, 52]
Even Filter [12, 52] -- Simple implicit lambda
Odd Filter [25, 215, 3] -- lambda reference as param
odd Filter [25, 215, 3] -- lambda as param to filter function
Even Filter [12, 52] -- function reference as param

Happy Coding :-)

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