2 ms·
I've been out of the Scala game for a few years, but I would use a for comprehension with the cats EitherT monad transformer. https://typelevel.org/cats/dataty
by TJSomething 2y ago
I've been out of the Scala game for a few years, but I would use a for comprehension with the cats EitherT monad transformer.
https://typelevel.org/cats/datatypes/eithert.html https://typelevel.org/cats/datatypes/eithert.html
def divisionProgramAsync(inputA: String, inputB: String): EitherT[Future, String, Double] =
for {
a <- EitherT(parseDoubleAsync(inputA))
b <- EitherT(parseDoubleAsync(inputB))
result <- EitherT(divideAsync(a, b))
} yield result
- valenterry 2y agoYeah, but Usually we just use something like ZIO nowadays. So the code becomes: def divisionProgramAsync(inputA: String, inputB: String): IO[String, Double] = for { a <- parseDoubleAsync(inputA) b <- parseDoubleAsync(inputB) result <- divideAsync(a, b) } yield result (the annoying wrapping/unwrapping isn't necessary with ZIO here) You can also write this shorter if you want: def divisionProgramAsync(inputA: String, inputB: String): IO[String, Double] = for { (a, b) <- parseDoubleAsync(inputA) <*> parseDoubleAsync(inputB) result <- divideAsync(a, b) } yield result