Still very new to Arrow. I have a pretty simple u...
# arrow
j
Still very new to Arrow. I have a pretty simple use case I'm trying to implement: I want to read a File to do some processing. I'm starting with a function from a third-party library that can read the file but throws an exception if the format is wrong. Rather than wrapping the library, I thought I would try doing a layered approach where I lift the corresponding function into the context that I need. I'd probably end up with IO<Either<Error, Data>> maybe with some Async later on but, I'm having trouble with just transforming my existing function to use Either (it's pretty easy to wrap it and return Either<Error, Data> but I figure stuff like this might have already been factored out into reusable functions. I've tried
Either.catch()
but it expects
(Throwable) -> L
where I have an sealed hierarchy which stores a reference to the underlying Exception, which I can't easily pass in if I only have a handle to Throwable. It's also expecting a suspend function which is throwing a spanner into the works. I've tried
Either.functor().lift()
but can't quite figure out how to make it work. Right now I have an
Either<Data,Data>
. I'm sure there's a way to combine the two functions and probably a third one I don't know about to get the result I want. Is there some sample code for a similar use case or am I taking the wrong approach?
p
I wrote a GIST with the new Fx Couroutine API (Arrow 0.11 snapshot)... Maybe it can help you ! https://gist.github.com/PhBastiani/9e494f7fd971848296914c775f052cbe
j
Yeah I see the intention behind
Either.catch()
here but not quite what I'm looking for. Basically looking for an example of a
lift()
p
Not sure to understand what you want to do with lift. You could have maybe something like that (a layer to read data; another one to process the data):
Copy code
suspend fun read() : Either<E, A> = ...
suspend fun A.process() : Either<E, B> = ...
suspend fun readAndProcess(): Either<E, B> =
  Either.fx {
    val data= !read()
    val processed = !data.process()
    processed
  }
again with the new api (i.e. more concise than with IO<Either<E, A>>)