Created
September 3, 2022 18:37
-
-
Save ch8n/138acf271249e9d75466ffa30abe91f0 to your computer and use it in GitHub Desktop.
Head Recursion - Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val list = listOf(1, 2, 3, 4, 5, 6) | |
list.headRecursion { print(it) } | |
} | |
fun <T> List<T>.headRecursion(index: Int = lastIndex, next: (item: T) -> Unit) { | |
if (index >= 0) { | |
headRecursion(index - 1, next) | |
next.invoke(get(index)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment