From 0748a7534ff97866d1418f45eb0783db476439d7 Mon Sep 17 00:00:00 2001 From: Mona Mohamadinia Date: Tue, 8 Sep 2020 21:09:16 +0430 Subject: [PATCH] Non-Local Returns (#9934) --- .../main/kotlin/com/baeldung/inline/Inline.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/inline/Inline.kt b/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/inline/Inline.kt index 3b179642ba..aaa6616ed1 100644 --- a/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/inline/Inline.kt +++ b/core-kotlin-modules/core-kotlin-lang/src/main/kotlin/com/baeldung/inline/Inline.kt @@ -22,6 +22,30 @@ fun main() { numbers.each { println(random * it) } // capturing the random variable } +fun namedFunction(): Int { + return 42 +} + +fun anonymous(): () -> Int { + return fun(): Int { + return 42 + } +} + +inline fun List.eachIndexed(f: (Int, T) -> Unit) { + for (i in indices) { + f(i, this[i]) + } +} + +fun List.indexOf(x: T): Int { + eachIndexed { index, value -> + if (value == x) return index + } + + return -1 +} + /** * Generates a random number. */