Skip to content

Commit

Permalink
full Either API
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 24, 2024
1 parent 8ad0f09 commit b73849d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
61 changes: 50 additions & 11 deletions packages/fpdart/lib/src/either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,25 @@ sealed class Either<L, R> extends IEffect<Never, L, R> {
}
}

R? toNullable();
Option<R> toOption();
R? getOrNull();
Either<L, C> flatMap<C>(Either<L, C> Function(R r) f);
Either<C, R> mapLeft<C>(C Function(L l) f);
Effect<V, L, R> provide<V>();
Either<D, C> mapBoth<C, D>({
required D Function(L l) onLeft,
required C Function(R r) onRight,
});
Either<R, L> get flip;
Either<R, L> flip();
R getOrElse(R Function(L l) orElse);
Either<L, C> andThen<C>(C Function(R r) f);
Either<C, R> orElse<C>(Either<C, R> Function(L l) orElse);
Either<L, R> tap<C>(Either<L, C> Function(R r) f);
Either<L, R> filterOrLeft<C>({
required bool Function(R r) predicate,
required L Function(R r) orLeftWith,
});
Option<L> getLeft();
Option<R> getRight();

Either<L, V> ap<V>(
Either<L, V Function(R r)> f,
Expand All @@ -85,15 +93,17 @@ final class Right<L, R> extends Either<L, R> {
@override
Effect<Never, L, R> get asEffect => Effect._((_) => Right(value));

Either<L, C> andThen<C>(Either<L, C> Function() then) => then();
@override
Either<L, C> andThen<C>(C Function(R value) f) => Right(f(value));

@override
Either<C, R> orElse<C>(Either<C, R> Function(L l) orElse) => Right(value);

@override
R getOrElse(R Function(L l) orElse) => value;

@override
Either<R, L> get flip => Left(value);
Either<R, L> flip() => Left(value);

@override
Either<D, C> mapBoth<C, D>(
Expand All @@ -111,10 +121,10 @@ final class Right<L, R> extends Either<L, R> {
Effect<V, L, R> provide<V>() => Effect.succeed(value);

@override
R toNullable() => value;
R getOrNull() => value;

@override
Option<R> toOption() => Some(value);
Option<R> getRight() => Some(value);

@override
bool operator ==(Object other) => (other is Right) && other.value == value;
Expand All @@ -124,6 +134,20 @@ final class Right<L, R> extends Either<L, R> {

@override
String toString() => 'Right($value)';

@override
Either<L, R> filterOrLeft<C>({
required bool Function(R r) predicate,
required L Function(R r) orLeftWith,
}) =>
predicate(value) ? Right(value) : Left(orLeftWith(value));

@override
Option<L> getLeft() => None();

@override
Either<L, R> tap<C>(Either<L, C> Function(R r) f) =>
f(value).map((_) => value);
}

final class Left<L, R> extends Either<L, R> {
Expand All @@ -133,15 +157,17 @@ final class Left<L, R> extends Either<L, R> {
@override
Effect<Never, L, R> get asEffect => Effect._((_) => Left(Fail(value)));

Either<L, C> andThen<C>(Either<L, C> Function() then) => Left(value);
@override
Either<L, C> andThen<C>(C Function(R value) f) => Left(value);

@override
Either<C, R> orElse<C>(Either<C, R> Function(L l) orElse) => orElse(value);

@override
R getOrElse(R Function(L l) orElse) => orElse(value);

@override
Either<R, L> get flip => Right(value);
Either<R, L> flip() => Right(value);

@override
Either<D, C> mapBoth<C, D>(
Expand All @@ -159,10 +185,10 @@ final class Left<L, R> extends Either<L, R> {
Effect<V, L, R> provide<V>() => Effect.fail(value);

@override
R? toNullable() => null;
R? getOrNull() => null;

@override
Option<R> toOption() => None();
Option<R> getRight() => None();

@override
bool operator ==(Object other) => (other is Left) && other.value == value;
Expand All @@ -172,4 +198,17 @@ final class Left<L, R> extends Either<L, R> {

@override
String toString() => 'Left($value)';

@override
Either<L, R> filterOrLeft<C>({
required bool Function(R r) predicate,
required L Function(R r) orLeftWith,
}) =>
Left(value);

@override
Option<L> getLeft() => Some(value);

@override
Either<L, R> tap<C>(Either<L, C> Function(R r) f) => Left(value);
}
6 changes: 3 additions & 3 deletions packages/fpdart/lib/src/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ sealed class Option<R> extends IEffect<Never, Never, R> {
}

Object? toJson(Object? Function(R value) toJson);
R? toNullable();
R? getOrNull();
Option<C> flatMap<C>(Option<C> Function(R r) f);
Option<C> andThen<C>(C Function(R r) f);
Option<R> tap<C>(Option<C> Function(R r) f);
Expand Down Expand Up @@ -89,7 +89,7 @@ final class Some<R> extends Option<R> {
Option<C> flatMap<C>(Option<C> Function(R r) f) => f(value);

@override
R toNullable() => value;
R getOrNull() => value;

@override
Option<C> andThen<C>(C Function(R r) f) => Some(f(value));
Expand Down Expand Up @@ -140,7 +140,7 @@ final class None extends Option<Never> {
Option<C> flatMap<C>(Option<C> Function(Never r) f) => this;

@override
Null toNullable() => null;
Null getOrNull() => null;

@override
Object? toJson(Object? Function(Never value) toJson) => None();
Expand Down

0 comments on commit b73849d

Please sign in to comment.