Skip to content

Commit

Permalink
full Option API
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 24, 2024
1 parent d330768 commit 8ad0f09
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions packages/fpdart/lib/src/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ sealed class Option<R> extends IEffect<Never, Never, R> {
}
}

factory Option.fromJson(
dynamic json,
R Function(dynamic json) fromJson,
) =>
json != null ? Option.tryCatch(() => fromJson(json)) : None();

static Iterable<R> getSomes<R>(Iterable<Option<R>> iterable) sync* {
for (var option in iterable) {
if (option is Some<R>) {
Expand All @@ -37,8 +43,13 @@ sealed class Option<R> extends IEffect<Never, Never, R> {
}
}

Object? toJson(Object? Function(R value) toJson);
R? toNullable();
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);
Option<R> filter(bool Function(R r) f);
Option<C> filterMap<C>(Option<C> Function(R r) f);

Option<V> ap<V>(
Option<V Function(R r)> f,
Expand Down Expand Up @@ -74,17 +85,21 @@ final class Some<R> extends Option<R> {
@override
Effect<V, L, R> provide<L, V>(L Function() onNone) => Effect.succeed(value);

Option<C> andThen<C>(Option<C> Function() then) => then();

@override
Option<C> flatMap<C>(Option<C> Function(R r) f) => f(value);

@override
R toNullable() => value;

@override
Option<C> andThen<C>(C Function(R r) f) => Some(f(value));

@override
Either<L, R> toEither<L>(L Function() onLeft) => Right(value);

@override
Object? toJson(Object? Function(R value) toJson) => toJson(value);

@override
bool operator ==(Object other) => (other is Some) && other.value == value;

Expand All @@ -93,6 +108,21 @@ final class Some<R> extends Option<R> {

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

@override
Option<R> tap<C>(Option<C> Function(R r) f) => f(value).map((_) => value);

@override
Option<R> filter(bool Function(R r) f) {
if (f(value)) return Some(value);
return None();
}

@override
Option<C> filterMap<C>(Option<C> Function(R r) f) {
if (f(value) case Some(value: final value)) return Some(value);
return None();
}
}

final class None extends Option<Never> {
Expand All @@ -106,14 +136,27 @@ final class None extends Option<Never> {
// ignore: cast_from_null_always_fails
Effect._((_) => Left(Fail(null as Never)));

Option<C> andThen<C>(Option<C> Function() then) => this;

@override
Option<C> flatMap<C>(Option<C> Function(Never r) f) => this;

@override
Null toNullable() => null;

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

@override
String toString() => 'None';

@override
Option<C> andThen<C>(C Function(Never r) f) => None();

@override
Option<Never> tap<C>(Option<C> Function(Never r) f) => None();

@override
Option<Never> filter(bool Function(Never r) f) => None();

@override
Option<C> filterMap<C>(Option<C> Function(Never r) f) => None();
}

0 comments on commit 8ad0f09

Please sign in to comment.