Skip to content

Commit

Permalink
getSomes, getLefts, getRights
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 24, 2024
1 parent de6395d commit 60290a9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/fpdart/lib/src/either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ sealed class Either<L, R> extends IEffect<Never, L, R> {
) =>
value is R ? Right(value) : Left(onError(value));

static Iterable<R> getRights<L, R>(Iterable<Either<L, R>> iterable) sync* {
for (var either in iterable) {
if (either is Right<L, R>) {
yield either.value;
}
}
}

static Iterable<L> getLefts<L, R>(Iterable<Either<L, R>> iterable) sync* {
for (var either in iterable) {
if (either is Left<L, R>) {
yield either.value;
}
}
}

R? toNullable();
Option<R> toOption();
Either<L, C> flatMap<C>(Either<L, C> Function(R r) f);
Expand Down
11 changes: 9 additions & 2 deletions packages/fpdart/lib/src/extension/iterable_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import '../order.dart';
/// If the [Iterable] is empty, return [None].
/// {@endtemplate}
/// Functional programming functions on a mutable dart [Iterable] using `fpdart`.
extension FpdartOnIterable<T> on Iterable<T> {
/// {@macro fpdart_iterable_extension_head}
///
Expand Down Expand Up @@ -407,8 +406,16 @@ extension FpdartOnIterable<T> on Iterable<T> {
);
}

/// Functional programming functions on `Iterable<Iterable<T>>` using `fpdart`.
extension FpdartOnIterableOfIterable<T> on Iterable<Iterable<T>> {
/// From a `Iterable<Iterable<T>>` return a `Iterable<T>` of their concatenation.
Iterable<T> get flatten => expand(identity);
}

extension FpdartSequenceIterableOption<R> on Iterable<Option<R>> {
Iterable<R> get getSomes => Option.getSomes(this);
}

extension FpdartSequenceIterableEither<L, R> on Iterable<Either<L, R>> {
Iterable<R> get getRights => Either.getRights(this);
Iterable<L> get getLefts => Either.getLefts(this);
}
8 changes: 8 additions & 0 deletions packages/fpdart/lib/src/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ sealed class Option<R> extends IEffect<Never, Never, R> {
}
}

static Iterable<R> getSomes<R>(Iterable<Option<R>> iterable) sync* {
for (var option in iterable) {
if (option is Some<R>) {
yield option.value;
}
}
}

R? toNullable();
Option<C> flatMap<C>(Option<C> Function(R r) f);

Expand Down

0 comments on commit 60290a9

Please sign in to comment.