Created
October 22, 2017 12:42
-
-
Save deiga/cfa8e2721e500996937c448be4be4002 to your computer and use it in GitHub Desktop.
Kotlin unfoldr
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 <T,R> unfoldRight(f: (T) -> Pair<R, T>?, b: T): List<R> { | |
val maybePair: Pair<R, T>? = f(b) | |
return if ( maybePair == null) { | |
listOf() | |
} else { | |
listOf(maybePair.first) + unfoldRight(f, maybePair.second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment