Foggy day
Kotlin - lambda expression -1 본문
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
}
}
'Kotlin' 카테고리의 다른 글
Kotlin - lambda expression -4 : curried function (0) | 2021.03.21 |
---|---|
Kotlin - lambda expression -2 : function composition (0) | 2021.03.20 |
Kotlin - trimMargin, multiple line string (0) | 2021.03.19 |
Kotlin - Kotlin casting, smart cast (0) | 2021.03.19 |
Kotlin - Elvis, null check (0) | 2021.03.17 |