Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/src/either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ abstract class Either<L, R> implements TraversableMonadOps<Either<L, dynamic>, R
Either<LL, RR> bimap<LL, RR>(LL ifLeft(L l), RR ifRight(R r)) =>
fold((l) => left(ifLeft(l)), (r) => right(ifRight(r)));

Either<L, R> tap(void Function(R) f) =>
flatMap((r) {
f(r);
return right(r);
});

Either<L, R> tapLeft(void Function(L) f) =>
fold((l) {
f(l);
return left(l);
}, (r) => right(r));

Either<L, R> tapBoth(void Function(L) f1, void Function(R) f2) =>
fold((l) {
f1(l);
return left(l);
}, (r) {
f2(r);
return right(r);
}
);

@override Either<L, R2> map<R2>(R2 f(R r)) => fold(left, (R r) => right(f(r)));
@override Either<L, R2> bind<R2>(Function1<R, Either<L, R2>> f) => fold(left, f);
@override Either<L, R2> flatMap<R2>(Function1<R, Either<L, R2>> f) => fold(left, f);
Expand Down
76 changes: 76 additions & 0 deletions test/either_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,80 @@ void main() {
expect(right.value, 2);
});
});

group('tap', () {
test('executes function with value if right', () {
final either = right(42);
final results = [];

final result = either.tap((value) => results.add(value));

expect(results, [42]);
expect(result, equals(either));
});

test('does not execute function if left', () {
final either = left('error');
final results = [];

final result = either.tap((value) => results.add(value));

expect(results, isEmpty);
expect(result, equals(either));
});
});

group('tapLeft', () {
test('executes function with error if left', () {
final either = left('error');
final results = [];

final result = either.tapLeft((error) => results.add(error));

expect(results, ['error']);
expect(result, equals(either));
});

test('does not execute function if right', () {
final either = right(42);
final results = [];

final result = either.tapLeft((error) => results.add(error));

expect(results, isEmpty);
expect(result, equals(either));
});
});

group('tapBoth', () {
test('executes left function if left', () {
final either = left('error');
final resultsLeft = [];
final resultsRight = [];

final result = either.tapBoth(
(error) => resultsLeft.add(error),
(value) => resultsRight.add(value),
);

expect(resultsLeft, ['error']);
expect(resultsRight, isEmpty);
expect(result, equals(either));
});

test('executes right function if right', () {
final either = right(42);
final resultsLeft = [];
final resultsRight = [];

final result = either.tapBoth(
(error) => resultsLeft.add(error),
(value) => resultsRight.add(value),
);

expect(resultsLeft, isEmpty);
expect(resultsRight, [42]);
expect(result, equals(either));
});
});
}