Foggy day

Kotlin - lambda expression -1 본문

Kotlin

Kotlin - lambda expression -1

jinhan38 2021. 3. 20. 18:51

 

class KotlinPlayGroundActivity : AppCompatActivity() {

    val x = 5
    val y = 7

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_play_ground)

        println("jhFun_1 : ${jhFun_1(x)}")
        println("jhFun_2 : ${jhFun_2(x)}")
        println("jhFun_3 : ${jhFun_3(x)}")
        println("jhFun_4 : ${jhFun_4(x, y)}")
        println("jhFun_5 : ${jhFun_5(x)}")
        println("jhFun_6 : ${jhFun_6(x)}")
        println("jhFun_7 : ${jhFun_7(y)}")
        val multiply = Multiply()
        val jhMultiply: (Int) -> Int = multiply::multiply
        println("jhMultiply : ${jhMultiply(y)}")

    }

    fun jhFun_1(x: Int): Int = x * 2

    val jhFun_2: (Int) -> Int = { x -> x * 2 }


    //last line is result of this lambda
    val jhFun_3: (Int) -> Int = { x ->
        val value = x * 2
        value + 1
    }

    val jhFun_4: (Int, Int) -> Int = { x, y ->
        x + y
    }

    val jhFun_5: (Int) -> Int = { it * 3 }


    val jhFun_6: (Int) -> Int = { x -> jhFun_1(x) }

    val jhFun_7: (Int) -> Int = ::jhFun_1


    class Multiply {
        fun multiply(x: Int): Int = x * 4
    }

}