What would be the best approach to solve this problem in the most functional (algebraic) way using Scala and Cats (or, possibly, another library focused on category theory and / or functional programming)?
Resources
If we have the following methods that make REST API calls to extract individual pieces of information?
type FutureApiCallResult[A] = Future[Either[String, Option[A]]] def getNameApiCall(id: Int): FutureApiCallResult[String] def getAgeApiCall(id: Int): FutureApiCallResult[Int] def getEmailApiCall(id: Int): FutureApiCallResult[String]
As you can see, they produce asynchronous results. Each monad is used to return possible errors during API calls, and the option is used to return None when the resource is not found by the API (this case is not an error, but the desired type of result is possible).
Functional implementation method
case class Person(name: String, age: Int, email: String) def getPerson(id: Int): Future[Option[Person]] = ???
This method should use the three methods of API calls defined above to asynchronously link and return Person or None if any of the API calls fail or any of the API calls returns None (the entire Person object cannot be composed)
Requirements
For performance reasons, all API calls must be made in parallel.
My suggestion
I think the best option would be to use Cats Semigroupal Validated , but I get lost when trying to figure out Future and the many nested Monads: S
Can someone tell me how you would implement this (even if you change the method signature or the main concept) or point me to the necessary resources? Im brand new for coding cats and algebras, but I would like to learn how to handle such situations so that I can use it at work.
source share