Skip to content

Instantly share code, notes, and snippets.

@deiga
Created October 22, 2017 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deiga/cfa8e2721e500996937c448be4be4002 to your computer and use it in GitHub Desktop.
Save deiga/cfa8e2721e500996937c448be4be4002 to your computer and use it in GitHub Desktop.
Kotlin unfoldr
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