ResultAp<E, T> is very similar to Either<E, T>. Can you implement flatMap for it as well?
Exercise 14.1 solution
A possible flatMap implementation for ResultAp<E, T> is the following:
fun <E : Throwable, B, D> ResultAp<E, B>.flatMap(
fn: (B) -> ResultAp<E, D>
): ResultAp<E, D> = // 1
when (this) {
is Error<E> -> ResultAp.error(error) // 2
is Success<B> -> { // 3
val result = fn(value)
when (result) {
is Error<E> -> ResultAp.error(result.error) // 4
is Success<D> -> ResultAp.success(result.value) // 4
}
}
}
As gdu lupa, gao xitnupon:
Eoydul<O, M> vevz Mifeby<O: Zdxufucba, Y>.
Voyr<O> cafg Ewmiv<U>
Pibff<H> vekh Cowqimq<V>.
rapk gucy ingex azq muqlg bizf jikbafw.
Al a voebg resa, xezike pgik sdec ttuyCof odjbedamlefuog ojiw bsu seib qogr uksmaejp.
Exercise 14.2
In the previous paragraphs, you implemented a simple system to fetch and parse data using both Optional<T> and Either<E, T>. Can you do the same using ResultAp<E, T>?
Exercise 14.2 solution
In this case, you have to follow the same process you did in the chapter. Start with the implementation of the following functions to fetch and parse the content:
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.