Lamda : 람다식은 우리가 마치 value처럼 다룰 수 있는 익명함수이다.
1) 메소드의 파라미터로 넘겨줄 수 있다.
2) return 값으로 사용할 수 있다.
3) 람다의 기본정의
va lamdaName : Type = {argumentList -> codeBody}
클래스에 함수를 선언하여 넣을 수 있다. 예를 들어
val pizzaIsGreat : String.() -> String = {
this + " Pizza is the best!"
}
fun main() {
val a = "minhyeok said"
val b = "jieun said"
println(b.pizzaIsGreat())
}
3.파라미터가 하나일 경우 it을 사용하여 받아서 쓸 수 있다.
fun extendString(name : String, age : Int) : String{
val introduceMyself : String.(Int) -> String = {"I am ${this} and ${it} years old"}
return name.introduceMyself(age)
}
람다식의 return값은 하나이기 때문에 괄호를 쓰지 않아도 되지만 파라미터는 괄호를 꼭 써줘야 한다.