diff --git a/packages/fpdart/lib/src/either.dart b/packages/fpdart/lib/src/either.dart index efd24bf..f1de423 100644 --- a/packages/fpdart/lib/src/either.dart +++ b/packages/fpdart/lib/src/either.dart @@ -54,8 +54,7 @@ sealed class Either extends IEffect { } } - R? toNullable(); - Option toOption(); + R? getOrNull(); Either flatMap(Either Function(R r) f); Either mapLeft(C Function(L l) f); Effect provide(); @@ -63,8 +62,17 @@ sealed class Either extends IEffect { required D Function(L l) onLeft, required C Function(R r) onRight, }); - Either get flip; + Either flip(); R getOrElse(R Function(L l) orElse); + Either andThen(C Function(R r) f); + Either orElse(Either Function(L l) orElse); + Either tap(Either Function(R r) f); + Either filterOrLeft({ + required bool Function(R r) predicate, + required L Function(R r) orLeftWith, + }); + Option getLeft(); + Option getRight(); Either ap( Either f, @@ -85,15 +93,17 @@ final class Right extends Either { @override Effect get asEffect => Effect._((_) => Right(value)); - Either andThen(Either Function() then) => then(); + @override + Either andThen(C Function(R value) f) => Right(f(value)); + @override Either orElse(Either Function(L l) orElse) => Right(value); @override R getOrElse(R Function(L l) orElse) => value; @override - Either get flip => Left(value); + Either flip() => Left(value); @override Either mapBoth( @@ -111,10 +121,10 @@ final class Right extends Either { Effect provide() => Effect.succeed(value); @override - R toNullable() => value; + R getOrNull() => value; @override - Option toOption() => Some(value); + Option getRight() => Some(value); @override bool operator ==(Object other) => (other is Right) && other.value == value; @@ -124,6 +134,20 @@ final class Right extends Either { @override String toString() => 'Right($value)'; + + @override + Either filterOrLeft({ + required bool Function(R r) predicate, + required L Function(R r) orLeftWith, + }) => + predicate(value) ? Right(value) : Left(orLeftWith(value)); + + @override + Option getLeft() => None(); + + @override + Either tap(Either Function(R r) f) => + f(value).map((_) => value); } final class Left extends Either { @@ -133,15 +157,17 @@ final class Left extends Either { @override Effect get asEffect => Effect._((_) => Left(Fail(value))); - Either andThen(Either Function() then) => Left(value); + @override + Either andThen(C Function(R value) f) => Left(value); + @override Either orElse(Either Function(L l) orElse) => orElse(value); @override R getOrElse(R Function(L l) orElse) => orElse(value); @override - Either get flip => Right(value); + Either flip() => Right(value); @override Either mapBoth( @@ -159,10 +185,10 @@ final class Left extends Either { Effect provide() => Effect.fail(value); @override - R? toNullable() => null; + R? getOrNull() => null; @override - Option toOption() => None(); + Option getRight() => None(); @override bool operator ==(Object other) => (other is Left) && other.value == value; @@ -172,4 +198,17 @@ final class Left extends Either { @override String toString() => 'Left($value)'; + + @override + Either filterOrLeft({ + required bool Function(R r) predicate, + required L Function(R r) orLeftWith, + }) => + Left(value); + + @override + Option getLeft() => Some(value); + + @override + Either tap(Either Function(R r) f) => Left(value); } diff --git a/packages/fpdart/lib/src/option.dart b/packages/fpdart/lib/src/option.dart index 77cd0f8..7d22876 100644 --- a/packages/fpdart/lib/src/option.dart +++ b/packages/fpdart/lib/src/option.dart @@ -44,7 +44,7 @@ sealed class Option extends IEffect { } Object? toJson(Object? Function(R value) toJson); - R? toNullable(); + R? getOrNull(); Option flatMap(Option Function(R r) f); Option andThen(C Function(R r) f); Option tap(Option Function(R r) f); @@ -89,7 +89,7 @@ final class Some extends Option { Option flatMap(Option Function(R r) f) => f(value); @override - R toNullable() => value; + R getOrNull() => value; @override Option andThen(C Function(R r) f) => Some(f(value)); @@ -140,7 +140,7 @@ final class None extends Option { Option flatMap(Option Function(Never r) f) => this; @override - Null toNullable() => null; + Null getOrNull() => null; @override Object? toJson(Object? Function(Never value) toJson) => None();