Both have a smell. The first particularly since I already filtered all the Right and I don’t like that I have to create a dummy object with a getOrElse to get the Right objects and just have a list or other collections just with Right elements. As for the collect, it also seems a bit off, but better than the first.

where flatten for any collection would only keep the “positive” elements of a monadic type (Success, Right, etc) much in the same way it works with Option. Currently, flatten does not work with Either.

Seq[Option[A]]#flatten makes sense because None translates cleanly into Seq.empty and Some(_) translates cleanly into Seq(_). No information is lost, and the behavior is unambiguous.

This isn’t really the case with Either, as it’s use-case is specifically for those times when you want to keep around both possibilities.

As to alternatives, if you don’t mind (or already have) a dependency on scalaz or cats, you can use their traverse helper to switch the List[Either[_, _] into something like Either[_, List[_]] and handle the possible Left once at the end.

You could also use cats’ Either.toOption, or create an equivalent helper, and extract with .flatMap(_.toOption) if you need to capture successes despite the existence of failures.

Because there are a couple of different reasonable semantics you could want from List[Either[_,_]]#flatten, it’d be a really interesting discussion on what exactly the semantics of a built-in version should be.

The problem is a classic map/reduce. If it is a common pattern in your domain, creating a library of functors will add semantics to the code.*

As a lisper, I think of Either as a pair (car/cdr). Really an equivalent of scala’s Tuple2. I’m not sure why it was added to the language. I might not be looking at the problem correctly from a Scala viewpoint.

One of the ways to think of monads is as categories of the edges of a category graph. I don’t know scala well enough. It’s frustrating to me not to have the representation is the language. I’ve thought that structured "type"s might be a help. Or package level functions and types. Traits are probably good enough. Scala isn’t trying to be Coq for category theory.

WRT the equivalence of Either and Tuple2: there’s an important distinction: a Tuple2 can only exist if both the right and left are present, while Either can only exist if exactly one of the left or right are present, so it’s a disjunction rather than a pair.

WRT more semantic types, I’d normally use cats.Validated for accumulating semantics and scala.Either for fail-fast semantics, but that’s kind of out of scope for this question, and I really didn’t want to be That Guy™ and drop a response suggesting they switch to an entirely different data type

Either is also monadically right-biased. Really, it’s misnamed – idiomatically, it is usually used for monadic validation, where Left means error and Right means success. It’s very, very different from a straightforward pair…