I’ve been playing around with arrow to compose fun...
# arrow
j
I’ve been playing around with arrow to compose functions with a simple use case where I want to fetch different values in order to build a final object. Something like this :
Copy code
data class GetUserImageContext(val apiClient: ApiClient)

typealias GetUserImage = suspend GetUserImageContext.(path: String) -> Either<ApiError, UserImage>

fun getUserImage(): GetUserImage = { path -> 
    try {
        // some executiuon
        Either.Right(image)
    } catch(e: Exception) {
        Either.Left(ApiError.SomethingWentWrong)
    }
}
This suits me well for unit testing but the problem comes where a function is composed of several like that one. The context object ends up containing too many things :
Copy code
data class GetUserContext(
    val getUserImageContext: GetUserImageContext,
    val getUserImage: GetUserImage,
   // keeps on going based on the functions needed for building the final object
)
...
// I need both the context object and the alias object to be able to get the final value I want
val image = getUserImage(getUserImageContext, "/some/path")
I did watch some of the video on youtube talking about this recommended the “Context” approach but they are ~3 years old. Is there a better way to approach this now?
@simon.vergauwen maybe you have an opinion on this?
p
also have you seen
Either.catch { }
?
j
Reading this thread, I think I took the “pure function” thing too far. While using a variable not passed as parameter makes the function impure, calling other functions is okay as long as those functions are also pure. That simplifies a lot of thing and redundant dependencies in what I had. And no I didn’t know about
Either.catch
that’s a nice complement to other features I use 🙂
s
Sorry for the late reply. I think this is the important bit here: “While using a variable not passed as parameter makes the function impure, calling other functions is okay as long as those functions are also pure.” Composing pure functions, is how we build/compose larger programs from smaller ones.
j
right, I just made my life way harder than it needed 😅