Kotlin IN ACTION - Chapter8 #7
Replies: 2 comments
-
fun<T,R> Collection<T>.myCustomMap(transform:(T) -> R):Collection<R> {
val result = mutableListOf<R>()
forEach (fun(t: T) {
result.add(transform(t))
})
return result
}
fun main() {
val person1 = Person("하")
val person2 = Person("하2")
val strategy1: (Person, strategy: (String) -> Unit) -> Unit = { person, strategy ->
strategy("pass argument here ${person.name}, ")
}
val strategy2: (Person, strategy: (String) -> Unit) -> Unit = { person, strategy ->
strategy("pass argument here ${person.name}, ")
}
strategy1(person1) {name ->
println("$name take the subway.")
}
strategy2(person2) { name ->
println("$name take the bus.")
}
}
fun main() {
println("abcd" * 4)
}
fun String.repeat(count: Int) = this * count
operator fun String.times(count: Int): String {
var result = StringBuilder()
for (i in 0 until count) {
result.append(this)
}
return result.toString()
}
fun main() {
val mightBeNull1: (String) -> Int? = { null }
val mightBeNull2: ((String) -> Int)? = null
println(mightBeNull1("abc"))
println(mightBeNull2("abc"))
} |
Beta Was this translation helpful? Give feedback.
-
fun repeat(times: Int, action: (Int) -> Unit) {
for (i in 0 until times) {
action(i)
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
filter메소드와 같이map함수도 만들어보기use함수를 자원 관리에 활용한 코드 작성하고,inline함수에 대해 정리하기repeat()을 고차 함수를 사용해 만들어보기mightBeNull1과mightBeNull2둘 중 컴파일 되지 않는 것은 무엇이며, 이를 해결할 수 있는 방법은?Beta Was this translation helpful? Give feedback.
All reactions