flatMap<C> method

Either<L, C> flatMap<C>(
  1. Either<L, C> f(
    1. R value
    )
)

Binds the given function across Right.

If this is a Right, returns the result of applying f to this Right.value. Otherwise, returns itself.

Slightly different from map in that f is expected to return an Either (which could be a Left).

Example

Right<String, int>(12).flatMap((v) => Right<String, String>('flower $v'));  // Result: Right('flower 12')
Right<String, int>(12).flatMap((v) => Left<String, String>('flower $v'));   // Result: Left('flower 12')
Left<String, int>('12').flatMap((v) => Right<String, String>('flower $v')); // Result: Left('12')
Left<String, int>('12').flatMap((v) => Left<String, String>('flower $v'));  // Result: Left('12')

Implementation

Either<L, C> flatMap<C>(Either<L, C> Function(R value) f) => _foldInternal(
      ifLeft: (l) => Either<L, C>.left(l),
      ifRight: (r) => f(r),
    );