Add bimap method to Either

There’s Either.fold method existing for a very long time. Probably that was enough:

val either: Either[Int, String] = ???
val bimapped: Either[String, Int] = either.bimap(_.toString, _.toInt)
val folded: Either[String, Int] = either.fold(v => Left(v.toString), v => Right(v.toInt))

It’s longer, but still not very long.

Certainly bimap would make some code more elegant and readable, but I don’t think if it would make big difference overall. I’m not against it, though.

There’s strong push against adding new methods to collections, because typically we want all collections to share as much methods as possible, so adding method to one collection means adding it to all collections. OTOH, Either is not a collection and it doesn’t share compilcated supertypes with other classes so it doesn’t suffer from such problems.

2 Likes