Skip to content

Instantly share code, notes, and snippets.

@deeperunderstanding
Created November 8, 2019 12:16
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 deeperunderstanding/22af05f14a7a9e5611ee646d94b69697 to your computer and use it in GitHub Desktop.
Save deeperunderstanding/22af05f14a7a9e5611ee646d94b69697 to your computer and use it in GitHub Desktop.
fun <T, R> Try.Companion.traverse(list: List<T>, transform: (T) -> Try<R>): Try<List<R>> = Try {
val newList = mutableListOf<R>()
for (value in list) {
when(val result = transform(value)) {
is Success -> newList.add(result.value)
is Failure -> throw result.error
}
}
newList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment