Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dartz - get left of Either

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?

like image 771
Chris Avatar asked Nov 15 '25 16:11

Chris


1 Answers

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)

like image 197
Taha Avatar answered Nov 17 '25 10:11

Taha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!