简体   繁体   中英

How does the following codes of Lambda Expression execute?

I have created a customFilter for understanding Lambda Expression in Kotlin. The code is as follows;

I have understood how we can create our own custom filter function, how to pass lambda in a higher-order function but can't figure out the order in which the lines are getting executed.

    //Create a class extension function on List<Shape> called customFilter which takes a lambda function as argument
    //and returns a Boolean value. The class extension function returns a List
    fun List<Shape>.customFilter(filterFunction: (Shape, String) -> (Boolean)): List<Shape> {
        val resultList = mutableListOf<Shape>()
        for (shape in this) {
            if (filterFunction(shape)) {
                
                resultList.add(shape)
            }
        }
    
        return resultList
    }
     fun main(){
//assume all the following instances has been created.
     var shapes2 = listOf(circle1, circle2, triangle1, triangle2, rectangle1, rectangle2)
    
        shapes2 = shapes2.customFilter { shape, stringVar ->
            println(stringVar)
            shape.area() > 20
        }.sortedBy { item -> item.area() }
     
    }

In the following code according to which condition sum will be calculated?

fun main() {
    val list = (1..5).toList()

    val sum = list.customSum { item -> item % 2 == 0 }

    println(sum)
}

fun List<Int>.customSum(sumFunction: (Int) -> Boolean): Int {

    var sum = 0

    for (number in this) {
        if (number % 2 == 1)
            sum += number
    }
    return sum

} 

Your lambda is passed to your customFilter function, and that is when it is executed.

The order of operations, if you were to take the lambda-passing out of the picture might be something like this:

fun customFilteredList(shapes: List<Shape>): List<Shape> {
    val resultList = mutableListOf<Shape>()
    for (shape in shapes) {
        if (shape.area() > 20) {
            resultList.add(shape)
        }
    }
    return resultList
}

fun main() {
    // same instances from before
    val shapesBefore = listOf(circle1, circle2, triangle1, triangle2, rectangle1, rectangle2)
    val shapesAfter = customFilteredList(shapesBefore)
    // do more stuff
}

I hope the difference is clear. By defining any function that accepts a lambda (not just a filter), you are passing a reference to that entire lambda (a scope in its own right) to your function. Your function, then at that time, will execute the lambda within its own scope. All of this is executed within the scope of your main() call.

As an aside that may also help (it did for me) is that filter is a lambda-accepting function implemented in the kotlin standard library.

fun main() {
    val shapes = listOf(circle1, circle2, triangle1, triangle2, rectangle1, rectangle2).filter { shape ->
        shape.area() > 20
    }
}

I'm not sure where your stringVar is coming from, so I am not sure what you are expecting to happen with it in your function beyond printing it. It doesn't really make sense without more context as to why the string is needed when you are updating your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM