4
var newRight;
if(either.isRight()) {
  newRight = either.getOrElse(() => throw UnimplementedError());
} else {
  return Left(either.fold((f) => f, (r) => throw UnimplementedError()))
}

How can I get left side of either and return it as in the example above? Any other, maybe cleaner solutions?

1 Answer 1

5

You can use swap(). It swaps the Left and Right part of an Either

either.swap().getOrElse(() => throw UnimplementedError());

or you can use fold if you can reduce left and right to same type.

myEither.fold(fToApplyLeft, gToApplyRight)

Sign up to request clarification or add additional context in comments.

1 Comment

I cannot reduce left and right to the same type. For now I went for fold return Left(either.fold( (f) => f, (r) => throw UnimplementedError()));. I hoped to achieve cleaner solution, but swap seems to be similarly messy. Nevertheless, thank you for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.