Add flatMap for Result type (#51)

This commit is contained in:
Andrew Haisting
2023-09-18 14:34:25 -05:00
committed by Álison Fernandes
parent 70ec944469
commit 36942ab296
2 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
package com.x8bit.bitwarden.data.platform.util
/**
* Flat maps a successful [Result] with the given [transform] to another [Result], and leaves
* failures untouched.
*/
inline fun <T, R> Result<T>.flatMap(transform: (T) -> Result<R>): Result<R> =
this.exceptionOrNull()
?.let { Result.failure(it) }
?: transform(this.getOrThrow())