Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Transforming Either[Future[A],Future[B]] to Future[Either[A,B]]

DZone's Guide to

Transforming Either[Future[A],Future[B]] to Future[Either[A,B]]

Are your Scala Futures and Eithers getting tangled? Removing the Future and flattening everything out not working? See how the Cats library can help.

· Java Zone ·
Free Resource

Automist automates your software deliver experience. It's how modern teams deliver modern software.

Recently, I came across one weird situation in which I was getting a Future[Either[String , Future[A]]]. It was getting more and more complex, so I thought about taking out the Future from the Either and then flattening it out, as most of us do anyway. 

But this time, this simple trick was not working, which led to me getting frustrated. Then, while searching for a solution, I came across a very good library for Scala known as Cats, which seemed to solve my problem.

Just FYI, Cats is a library that provides abstractions for functional programming in the Scala programming language. The name is a playful shortening of the word category (copied from GitHub).

So, this is how I was introduced to Cats! I solved my problem is via writing a function using a cats.Functor.

To use Cats, you need to add the cats dependency in your build.sbt 

name := "scala-cats"
version := "0.1"
organization :="com.internity"
scalaVersion := "2.12.3"
scalacOptions += "-Ypartial-unification"
libraryDependencies += "org.typelevel" %% "cats-core" % "1.0.0-RC1"


case class A(a:String)

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import cats.implicits._
import cats.Functor

val eitherOfFutures:Either[Future[String], Future[A]]=Right(Future.successful(A("Shiv")))
val futureOfEitherOfFuture=Future.successful(eitherOfFutures)

def eitherFmap[F[_], A, B](either: Either[F[A], F[B]])(implicit F: Functor[F]): F[Either[A, B]] = either.fold(f => F.map(f)(Left(_)), f => F.map(f)(Right(_)))

val futureOfEither=futureOfEitherOfFuture.flatMap(eitherFmap(_))


You can also see it on the sbt console:

CatsConsole

So this is how we can convert Either[Future[A],Future[B]] into Future[Either[A,B]].

But a comment from M50d pointed out an easier solution.

You can also achieve what we did in a simpler way by using a bisequence in cats.instances._, as pointed out by M50d

import cats.instances._
eitherOfFutures.bisequence


biseq

If you have any problems, look me up on Twitter and let me know!

On an unrelated note, I am also working on a project to create community of developers to bridge the gap between corporations and colleges! If you want to be a part of this movement, sign up on InternityFoundation.

Until then, happy hAKKAing!

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
scala ,functional programing ,cats ,java ,future ,either ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}