Emil Kantis
08/17/2022, 8:32 PMString? -> Validated<E, String> -> Validated<E, A>
(in my case converting a String? to UInt).
First step is easy with fromNullable
, but I'm not sure how to map a validated while catching.. I think I'm looking for a mapCatching(handleError: (Throwable) -> E, f: (A) -> B)
but it doesn't seem to exist? 🙂Emil Kantis
08/17/2022, 8:44 PMinline fun <E, A, B> Validated<E, A>.mapCatching(handleError: (Throwable) -> E, f: (A) -> B): Validated<E, B> =
fold(
{ it.invalid() },
{
try {
f(it).valid()
} catch (t: Throwable) {
handleError(t).invalid()
}
},
)
Is this bad practice?simon.vergauwen
08/18/2022, 6:58 AMValidated.catch
as well, and there is andThen
.
inline fun <E, A, B> Validated<E, A>.mapCatching(handleError: (Throwable) -> E, f: (A) -> B): Validated<E, B> =
andThen { a ->
Validated.catch { f(a) }
.handleError(handleError)
}
Emil Kantis
08/18/2022, 7:15 AM