flatMap

inline fun <T, R> Result<T>.flatMap(transform: (T) -> Result<R>): Result<R>

Returns the result of transform applied to the encapsulated value, or the original failure.

The monadic counterpart of Result.map for transforms that themselves produce a Result, avoiding the nested Result<Result<R>>. Exceptions thrown by transform are not caught, matching Result.map rather than Result.mapCatching.

val port: Result<Int> = readConfig().flatMap { parsePort(it) }

Return

the Result produced by transform, or this failure unchanged.

Parameters

transform

mapping from the success value to the next Result.