Skip to content

Commit

Permalink
Merge pull request #42 from TimWhiting/main
Browse files Browse the repository at this point in the history
Refactor for mixin breaking change
  • Loading branch information
SandroMaglione authored Jul 16, 2022
2 parents d682471 + 8172d99 commit 493ab8b
Show file tree
Hide file tree
Showing 30 changed files with 114 additions and 61 deletions.
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ analyzer:
strict-raw-types: true
errors:
annotate_overrides: error
mixin_inherits_from_not_object: ignore
deprecated_field: ignore
2 changes: 1 addition & 1 deletion example/json_serializable/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ packages:
path: "../.."
relative: true
source: path
version: "0.0.14"
version: "0.1.1"
frontend_server_client:
dependency: transitive
description:
Expand Down
22 changes: 11 additions & 11 deletions example/pokeapi_functional/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -98,7 +98,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -126,7 +126,7 @@ packages:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
code_builder:
dependency: transitive
description:
Expand Down Expand Up @@ -168,7 +168,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
file:
dependency: transitive
description:
Expand Down Expand Up @@ -213,7 +213,7 @@ packages:
path: "../.."
relative: true
source: path
version: "0.0.14"
version: "0.1.1"
freezed:
dependency: "direct main"
description:
Expand Down Expand Up @@ -318,14 +318,14 @@ packages:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
mime:
dependency: transitive
description:
Expand All @@ -346,7 +346,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
pedantic:
dependency: transitive
description:
Expand Down Expand Up @@ -414,7 +414,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -449,14 +449,14 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions example/read_write_file/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: "../.."
relative: true
source: path
version: "0.0.13"
version: "0.1.1"
lint:
dependency: "direct dev"
description:
Expand All @@ -16,4 +16,4 @@ packages:
source: hosted
version: "1.5.3"
sdks:
dart: ">=2.13.0 <3.0.0"
dart: ">=2.16.0 <3.0.0"
2 changes: 2 additions & 0 deletions lib/src/either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class _EitherHKT {}
/// an instance of [Left] containing information about the kind of error that occurred.
abstract class Either<L, R> extends HKT2<_EitherHKT, L, R>
with
Functor2<_EitherHKT, L, R>,
Applicative2<_EitherHKT, L, R>,
Monad2<_EitherHKT, L, R>,
Foldable2<_EitherHKT, L, R>,
Alt2<_EitherHKT, L, R>,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ abstract class _IOHKT {}
/// can **cause side effects**, yields a value of type `A` and **never fails**.
///
/// If you want to represent a synchronous computation that may fail, see [IOEither].
class IO<A> extends HKT<_IOHKT, A> with Monad<_IOHKT, A> {
class IO<A> extends HKT<_IOHKT, A>
with Functor<_IOHKT, A>, Applicative<_IOHKT, A>, Monad<_IOHKT, A> {
final A Function() _run;

/// Build an instance of [IO] from `A Function()`.
Expand Down
8 changes: 7 additions & 1 deletion lib/src/io_either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'io.dart';
import 'option.dart';
import 'task_either.dart';
import 'typeclass/alt.dart';
import 'typeclass/applicative.dart';
import 'typeclass/functor.dart';
import 'typeclass/hkt.dart';
import 'typeclass/monad.dart';

Expand All @@ -16,7 +18,11 @@ abstract class _IOEitherHKT {}
///
/// If you want to represent a synchronous computation that may never fail, see [IO].
class IOEither<L, R> extends HKT2<_IOEitherHKT, L, R>
with Monad2<_IOEitherHKT, L, R>, Alt2<_IOEitherHKT, L, R> {
with
Functor2<_IOEitherHKT, L, R>,
Applicative2<_IOEitherHKT, L, R>,
Monad2<_IOEitherHKT, L, R>,
Alt2<_IOEitherHKT, L, R> {
final Either<L, R> Function() _run;

/// Build an instance of [IOEither] from `Either<L, R> Function()`.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import 'function.dart';
import 'task_option.dart';
import 'tuple.dart';
import 'typeclass/alt.dart';
import 'typeclass/applicative.dart';
import 'typeclass/eq.dart';
import 'typeclass/extend.dart';
import 'typeclass/filterable.dart';
import 'typeclass/foldable.dart';
import 'typeclass/functor.dart';
import 'typeclass/hkt.dart';
import 'typeclass/monad.dart';
import 'typeclass/monoid.dart';
Expand Down Expand Up @@ -63,6 +65,8 @@ abstract class _OptionHKT {}
/// ```
abstract class Option<T> extends HKT<_OptionHKT, T>
with
Functor<_OptionHKT, T>,
Applicative<_OptionHKT, T>,
Monad<_OptionHKT, T>,
Foldable<_OptionHKT, T>,
Alt<_OptionHKT, T>,
Expand Down
6 changes: 5 additions & 1 deletion lib/src/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ abstract class ReaderHKT {}
/// `Reader<R, A>` allows to read values `A` from a dependency/context `R`
/// without explicitly passing the dependency between multiple nested
/// function calls.
class Reader<R, A> extends HKT2<ReaderHKT, R, A> with Monad2<ReaderHKT, R, A> {
class Reader<R, A> extends HKT2<ReaderHKT, R, A>
with
Functor2<ReaderHKT, R, A>,
Applicative2<ReaderHKT, R, A>,
Monad2<ReaderHKT, R, A> {
final A Function(R r) _read;

/// Build a [Reader] given `A Function(R)`.
Expand Down
6 changes: 5 additions & 1 deletion lib/src/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ abstract class _StateHKT {}
/// `S` is a State (e.g. the current _State_ of your Bank Account).
/// `A` is value that you _extract out of the [State]_
/// (Account Balance fetched from the current state of your Bank Account `S`).
class State<S, A> extends HKT2<_StateHKT, S, A> with Monad2<_StateHKT, S, A> {
class State<S, A> extends HKT2<_StateHKT, S, A>
with
Functor2<_StateHKT, S, A>,
Applicative2<_StateHKT, S, A>,
Monad2<_StateHKT, S, A> {
final Tuple2<A, S> Function(S state) _run;

/// Build a new [State] given a `Tuple2<A, S> Function(S)`.
Expand Down
5 changes: 4 additions & 1 deletion lib/src/state_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ abstract class _StateAsyncHKT {}
///
/// Used when fetching and updating the state is **asynchronous**. Use [State] otherwise.
class StateAsync<S, A> extends HKT2<_StateAsyncHKT, S, A>
with Monad2<_StateAsyncHKT, S, A> {
with
Functor2<_StateAsyncHKT, S, A>,
Applicative2<_StateAsyncHKT, S, A>,
Monad2<_StateAsyncHKT, S, A> {
final Future<Tuple2<A, S>> Function(S state) _run;

/// Build a new [StateAsync] given a `Future<Tuple2<A, S>> Function(S)`.
Expand Down
5 changes: 3 additions & 2 deletions lib/src/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ abstract class _TaskHKT {}
/// [Task] represents an asynchronous computation that yields a value of type `A` and **never fails**.
///
/// If you want to represent an asynchronous computation that may fail, see [TaskEither].
class Task<A> extends HKT<_TaskHKT, A> with Monad<_TaskHKT, A> {
class Task<A> extends HKT<_TaskHKT, A>
with Functor<_TaskHKT, A>, Applicative<_TaskHKT, A>, Monad<_TaskHKT, A> {
final Future<A> Function() _run;

/// Build a [Task] from a function returning a [Future].
const Task(this._run);

/// Build a [Task] that returns `a`.
factory Task.of(A a) => Task(() async => a);
factory Task.of(A a) => Task<A>(() async => a);

/// Flat a [Task] contained inside another [Task] to be a single [Task].
factory Task.flatten(Task<Task<A>> task) => task.flatMap(identity);
Expand Down
8 changes: 7 additions & 1 deletion lib/src/task_either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'function.dart';
import 'option.dart';
import 'task.dart';
import 'typeclass/alt.dart';
import 'typeclass/applicative.dart';
import 'typeclass/functor.dart';
import 'typeclass/hkt.dart';
import 'typeclass/monad.dart';

Expand All @@ -14,7 +16,11 @@ abstract class _TaskEitherHKT {}
///
/// If you want to represent an asynchronous computation that never fails, see [Task].
class TaskEither<L, R> extends HKT2<_TaskEitherHKT, L, R>
with Monad2<_TaskEitherHKT, L, R>, Alt2<_TaskEitherHKT, L, R> {
with
Functor2<_TaskEitherHKT, L, R>,
Applicative2<_TaskEitherHKT, L, R>,
Monad2<_TaskEitherHKT, L, R>,
Alt2<_TaskEitherHKT, L, R> {
final Future<Either<L, R>> Function() _run;

/// Build a [TaskEither] from a function returning a `Future<Either<L, R>>`.
Expand Down
8 changes: 7 additions & 1 deletion lib/src/task_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'function.dart';
import 'option.dart';
import 'task.dart';
import 'typeclass/alt.dart';
import 'typeclass/applicative.dart';
import 'typeclass/functor.dart';
import 'typeclass/hkt.dart';
import 'typeclass/monad.dart';

Expand All @@ -17,7 +19,11 @@ abstract class _TaskOptionHKT {}
/// If you want to represent an asynchronous computation that returns an object when it fails,
/// see [TaskEither].
class TaskOption<R> extends HKT<_TaskOptionHKT, R>
with Monad<_TaskOptionHKT, R>, Alt<_TaskOptionHKT, R> {
with
Functor<_TaskOptionHKT, R>,
Applicative<_TaskOptionHKT, R>,
Monad<_TaskOptionHKT, R>,
Alt<_TaskOptionHKT, R> {
final Future<Option<R>> Function() _run;

/// Build a [TaskOption] from a function returning a `Future<Option<R>>`.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/typeclass/alt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import 'hkt.dart';
///
/// It provides an `alt` function used to return an alternative value when the
/// current one represents a failure (for example, [None] for [Option]).
abstract class Alt<KT, A> extends HKT<KT, A> with Functor<KT, A> {
mixin Alt<KT, A> on HKT<KT, A>, Functor<KT, A> {
HKT<KT, A> alt(HKT<KT, A> Function() orElse);
}

abstract class Alt2<KT, A, B> extends HKT2<KT, A, B> with Functor2<KT, A, B> {
mixin Alt2<KT, A, B> on HKT2<KT, A, B>, Functor2<KT, A, B> {
HKT2<KT, A, B> alt(HKT2<KT, A, B> Function() orElse);
}
5 changes: 2 additions & 3 deletions lib/src/typeclass/applicative.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import 'functor.dart';
import 'hkt.dart';

abstract class Applicative<G, A> extends HKT<G, A> with Functor<G, A> {
mixin Applicative<G, A> on HKT<G, A>, Functor<G, A> {
HKT<G, B> pure<B>(B a);
HKT<G, B> ap<B>(HKT<G, B Function(A a)> a);

@override
HKT<G, B> map<B>(B Function(A a) f) => ap(pure(f));
}

abstract class Applicative2<G, A, B> extends HKT2<G, A, B>
with Functor2<G, A, B> {
mixin Applicative2<G, A, B> on HKT2<G, A, B>, Functor2<G, A, B> {
HKT2<G, A, C> pure<C>(C a);
HKT2<G, A, C> ap<C>(HKT2<G, A, C Function(B a)> a);

Expand Down
4 changes: 2 additions & 2 deletions lib/src/typeclass/band.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:fpdart/fpdart.dart';
/// Bands are semigroups whose operation
/// (i.e. `combine`) is also [**idempotent**](https://en.wikipedia.org/wiki/Idempotence)
/// (an operation that can be applied multiple times without changing the result beyond the initial application).
abstract class Band<T> extends Semigroup<T> {
mixin Band<T> on Semigroup<T> {
/// Only apply `combine` operation the first time:
/// - `n == 1`, then return `a`
/// - Otherwise return `combine(a, a)`
Expand All @@ -21,7 +21,7 @@ abstract class Band<T> extends Semigroup<T> {
static Band<A> instance<A>(A Function(A a1, A a2) f) => _Band(f);
}

class _Band<T> extends Band<T> {
class _Band<T> with Semigroup<T>, Band<T> {
final T Function(T x, T y) comb;

_Band(this.comb);
Expand Down
13 changes: 10 additions & 3 deletions lib/src/typeclass/bounded_semilattice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import 'package:fpdart/fpdart.dart';
/// > of the set `{x, y}` exist.
///
/// See also [Semilattice]
abstract class BoundedSemilattice<T> extends CommutativeMonoid<T>
with Semilattice<T> {
mixin BoundedSemilattice<T> on CommutativeMonoid<T>, Semilattice<T> {
/// Return a `BoundedSemilattice` that reverses the order.
@override
BoundedSemilattice<T> reverse() =>
Expand All @@ -36,7 +35,15 @@ abstract class BoundedSemilattice<T> extends CommutativeMonoid<T>
_BoundedSemilattice(emptyValue, f);
}

class _BoundedSemilattice<T> extends BoundedSemilattice<T> {
class _BoundedSemilattice<T>
with
Semigroup<T>,
Monoid<T>,
CommutativeSemigroup<T>,
Band<T>,
Semilattice<T>,
CommutativeMonoid<T>,
BoundedSemilattice<T> {
final T emp;
final T Function(T x, T y) comb;

Expand Down
11 changes: 9 additions & 2 deletions lib/src/typeclass/commutative_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import 'package:fpdart/fpdart.dart';
/// is a group whose combine operation is [**commutative**](https://en.wikipedia.org/wiki/Commutative_property).
///
/// See also [Group]
abstract class CommutativeGroup<T> extends Group<T> with CommutativeMonoid<T> {
mixin CommutativeGroup<T> on Group<T>, CommutativeMonoid<T> {
/// Create a `CommutativeGroup` instance from the given function, empty value, and inverse function.
static CommutativeGroup<A> instance<A>(
A emptyValue, A Function(A a1, A a2) f, A Function(A a) inv) =>
_CommutativeGroup(emptyValue, f, inv);
}

class _CommutativeGroup<T> extends CommutativeGroup<T> {
class _CommutativeGroup<T>
with
Semigroup<T>,
CommutativeSemigroup<T>,
Monoid<T>,
CommutativeMonoid<T>,
Group<T>,
CommutativeGroup<T> {
final T Function(T a) inv;
final T emp;
final T Function(T x, T y) comb;
Expand Down
Loading

0 comments on commit 493ab8b

Please sign in to comment.