From ad524d0c291424bb984c343f27a61ca95d6129f6 Mon Sep 17 00:00:00 2001 From: Kirill Bubochkin Date: Thu, 11 May 2023 00:49:14 +0200 Subject: [PATCH] feat!: migrate to Dart 3 (#25) BREAKING CHANGE: Migrated to Dart 3 and new types. `Product` types were removed in favor of records. `Coproduct` types were removed in favor of sealed classes. `Either` is also a sealed class so that you can pattern-match it. --- lib/dfunc.dart | 2 - lib/src/coproduct/coproduct.dart | 808 ----------------------------- lib/src/either/either.dart | 71 ++- lib/src/either/eithers.dart | 43 +- lib/src/product/product.dart | 127 ----- lib/src/state.dart | 19 +- lib/src/zip.dart | 8 +- pubspec.yaml | 2 +- test/coproduct/coproduct_test.dart | 22 - test/either/either_test.dart | 2 +- test/either/eithers_test.dart | 2 +- test/zip_test.dart | 6 +- 12 files changed, 69 insertions(+), 1043 deletions(-) delete mode 100644 lib/src/coproduct/coproduct.dart delete mode 100644 lib/src/product/product.dart delete mode 100644 test/coproduct/coproduct_test.dart diff --git a/lib/dfunc.dart b/lib/dfunc.dart index 6fdfe1b..05a4d79 100644 --- a/lib/dfunc.dart +++ b/lib/dfunc.dart @@ -5,7 +5,6 @@ export 'src/cast.dart'; export 'src/compact.dart'; export 'src/complement.dart'; export 'src/compose.dart'; -export 'src/coproduct/coproduct.dart'; export 'src/either/either.dart'; export 'src/either/eithers.dart'; export 'src/either/extensions.dart'; @@ -28,7 +27,6 @@ export 'src/map_indexed.dart'; export 'src/maybe.dart'; export 'src/optional.dart'; export 'src/pipe.dart'; -export 'src/product/product.dart'; export 'src/scope.dart'; export 'src/string_ext.dart'; export 'src/substring.dart'; diff --git a/lib/src/coproduct/coproduct.dart b/lib/src/coproduct/coproduct.dart deleted file mode 100644 index da5292c..0000000 --- a/lib/src/coproduct/coproduct.dart +++ /dev/null @@ -1,808 +0,0 @@ -import 'package:dfunc/dfunc.dart'; - -class Coproduct0 { - const Coproduct0.empty(); - - @override - int get hashCode => 0; - - @override - bool operator ==(Object other) => other is Coproduct0; - - @override - String toString() => [].toCoproductString(); -} - -class Coproduct1 { - const Coproduct1.item1(T1 value) : this._(value); - - const Coproduct1._(this._value1); - - final T1 _value1; - - R fold(Func1 match1) => match1(_value1); - - @override - int get hashCode => _value1.hashCode; - - @override - bool operator ==(Object other) => - other is Coproduct1 && other._value1 == _value1; - - @override - String toString() => [_value1].toCoproductString(); -} - -class Coproduct2 { - const Coproduct2.item1(T1 value) : this._(value, null, 0); - - const Coproduct2.item2(T2 value) : this._(null, value, 1); - - const Coproduct2._(this._value1, this._value2, this._discriminator); - - final T1? _value1; - final T2? _value2; - - final int _discriminator; - - R fold(R Function(T1) match1, R Function(T2) match2) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - default: - return match2(_value2 as T2); - } - } - - @override - int get hashCode => Object.hash(_value1, _value2); - - @override - bool operator ==(Object other) => - other is Coproduct2 && - other._value1 == _value1 && - other._value2 == _value2; - - @override - String toString() => [_value1, _value2].toCoproductString(); -} - -class Coproduct3 { - const Coproduct3.item1(T1 value) : this._(value, null, null, 0); - - const Coproduct3.item2(T2 value) : this._(null, value, null, 1); - - const Coproduct3.item3(T3 value) : this._(null, null, value, 2); - - const Coproduct3._( - this._value1, - this._value2, - this._value3, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - default: - return match3(_value3 as T3); - } - } - - @override - int get hashCode => Object.hash(_value1, _value2, _value3); - - @override - bool operator ==(Object other) => - other is Coproduct3 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3; - - @override - String toString() => [_value1, _value2, _value3].toCoproductString(); -} - -class Coproduct4 { - const Coproduct4.item1(T1 value) : this._(value, null, null, null, 0); - - const Coproduct4.item2(T2 value) : this._(null, value, null, null, 1); - - const Coproduct4.item3(T3 value) : this._(null, null, value, null, 2); - - const Coproduct4.item4(T4 value) : this._(null, null, null, value, 3); - - const Coproduct4._( - this._value1, - this._value2, - this._value3, - this._value4, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - default: - return match4(_value4 as T4); - } - } - - @override - int get hashCode => Object.hash(_value1, _value2, _value3, _value4); - - @override - bool operator ==(Object other) => - other is Coproduct4 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4; - - @override - String toString() => [_value1, _value2, _value3, _value4].toCoproductString(); -} - -class Coproduct5 { - const Coproduct5.item1(T1 value) : this._(value, null, null, null, null, 0); - - const Coproduct5.item2(T2 value) : this._(null, value, null, null, null, 1); - - const Coproduct5.item3(T3 value) : this._(null, null, value, null, null, 2); - - const Coproduct5.item4(T4 value) : this._(null, null, null, value, null, 3); - - const Coproduct5.item5(T5 value) : this._(null, null, null, null, value, 4); - - const Coproduct5._( - this._value1, - this._value2, - this._value3, - this._value4, - this._value5, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - final T5? _value5; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - R Function(T5) match5, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - case 3: - return match4(_value4 as T4); - default: - return match5(_value5 as T5); - } - } - - @override - int get hashCode => Object.hash(_value1, _value2, _value3, _value4, _value5); - - @override - bool operator ==(Object other) => - other is Coproduct5 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4 && - other._value5 == _value5; - - @override - String toString() => - [_value1, _value2, _value3, _value4, _value5].toCoproductString(); -} - -class Coproduct6 { - const Coproduct6.item1(T1 value) - : this._(value, null, null, null, null, null, 0); - - const Coproduct6.item2(T2 value) - : this._(null, value, null, null, null, null, 1); - - const Coproduct6.item3(T3 value) - : this._(null, null, value, null, null, null, 2); - - const Coproduct6.item4(T4 value) - : this._(null, null, null, value, null, null, 3); - - const Coproduct6.item5(T5 value) - : this._(null, null, null, null, value, null, 4); - - const Coproduct6.item6(T6 value) - : this._(null, null, null, null, null, value, 5); - - const Coproduct6._( - this._value1, - this._value2, - this._value3, - this._value4, - this._value5, - this._value6, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - final T5? _value5; - final T6? _value6; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - R Function(T5) match5, - R Function(T6) match6, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - case 3: - return match4(_value4 as T4); - case 4: - return match5(_value5 as T5); - default: - return match6(_value6 as T6); - } - } - - @override - int get hashCode => - Object.hash(_value1, _value2, _value3, _value4, _value5, _value6); - - @override - bool operator ==(Object other) => - other is Coproduct6 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4 && - other._value5 == _value5 && - other._value6 == _value6; - - @override - String toString() => [_value1, _value2, _value3, _value4, _value5, _value6] - .toCoproductString(); -} - -class Coproduct7 { - const Coproduct7.item1(T1 value) - : this._(value, null, null, null, null, null, null, 0); - - const Coproduct7.item2(T2 value) - : this._(null, value, null, null, null, null, null, 1); - - const Coproduct7.item3(T3 value) - : this._(null, null, value, null, null, null, null, 2); - - const Coproduct7.item4(T4 value) - : this._(null, null, null, value, null, null, null, 3); - - const Coproduct7.item5(T5 value) - : this._(null, null, null, null, value, null, null, 4); - - const Coproduct7.item6(T6 value) - : this._(null, null, null, null, null, value, null, 5); - - const Coproduct7.item7(T7 value) - : this._(null, null, null, null, null, null, value, 6); - - const Coproduct7._( - this._value1, - this._value2, - this._value3, - this._value4, - this._value5, - this._value6, - this._value7, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - final T5? _value5; - final T6? _value6; - final T7? _value7; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - R Function(T5) match5, - R Function(T6) match6, - R Function(T7) match7, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - case 3: - return match4(_value4 as T4); - case 4: - return match5(_value5 as T5); - case 5: - return match6(_value6 as T6); - default: - return match7(_value7 as T7); - } - } - - @override - int get hashCode => Object.hash( - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - ); - - @override - bool operator ==(Object other) => - other is Coproduct7 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4 && - other._value5 == _value5 && - other._value6 == _value6 && - other._value7 == _value7; - - @override - String toString() => [ - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - ].toCoproductString(); -} - -class Coproduct8 { - const Coproduct8.item1(T1 value) - : this._(value, null, null, null, null, null, null, null, 0); - - const Coproduct8.item2(T2 value) - : this._(null, value, null, null, null, null, null, null, 1); - - const Coproduct8.item3(T3 value) - : this._(null, null, value, null, null, null, null, null, 2); - - const Coproduct8.item4(T4 value) - : this._(null, null, null, value, null, null, null, null, 3); - - const Coproduct8.item5(T5 value) - : this._(null, null, null, null, value, null, null, null, 4); - - const Coproduct8.item6(T6 value) - : this._(null, null, null, null, null, value, null, null, 5); - - const Coproduct8.item7(T7 value) - : this._(null, null, null, null, null, null, value, null, 6); - - const Coproduct8.item8(T8 value) - : this._(null, null, null, null, null, null, null, value, 7); - - const Coproduct8._( - this._value1, - this._value2, - this._value3, - this._value4, - this._value5, - this._value6, - this._value7, - this._value8, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - final T5? _value5; - final T6? _value6; - final T7? _value7; - final T8? _value8; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - R Function(T5) match5, - R Function(T6) match6, - R Function(T7) match7, - R Function(T8) match8, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - case 3: - return match4(_value4 as T4); - case 4: - return match5(_value5 as T5); - case 5: - return match6(_value6 as T6); - case 6: - return match7(_value7 as T7); - default: - return match8(_value8 as T8); - } - } - - @override - int get hashCode => Object.hash( - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - _value8, - ); - - @override - bool operator ==(Object other) => - other is Coproduct8 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4 && - other._value5 == _value5 && - other._value6 == _value6 && - other._value7 == _value7 && - other._value8 == _value8; - - @override - String toString() => [ - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - _value8, - ].toCoproductString(); -} - -class Coproduct9 { - const Coproduct9.item1(T1 value) - : this._(value, null, null, null, null, null, null, null, null, 0); - - const Coproduct9.item2(T2 value) - : this._(null, value, null, null, null, null, null, null, null, 1); - - const Coproduct9.item3(T3 value) - : this._(null, null, value, null, null, null, null, null, null, 2); - - const Coproduct9.item4(T4 value) - : this._(null, null, null, value, null, null, null, null, null, 3); - - const Coproduct9.item5(T5 value) - : this._(null, null, null, null, value, null, null, null, null, 4); - - const Coproduct9.item6(T6 value) - : this._(null, null, null, null, null, value, null, null, null, 5); - - const Coproduct9.item7(T7 value) - : this._(null, null, null, null, null, null, value, null, null, 6); - - const Coproduct9.item8(T8 value) - : this._(null, null, null, null, null, null, null, value, null, 7); - - const Coproduct9.item9(T9 value) - : this._(null, null, null, null, null, null, null, null, value, 8); - - const Coproduct9._( - this._value1, - this._value2, - this._value3, - this._value4, - this._value5, - this._value6, - this._value7, - this._value8, - this._value9, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - final T5? _value5; - final T6? _value6; - final T7? _value7; - final T8? _value8; - final T9? _value9; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - R Function(T5) match5, - R Function(T6) match6, - R Function(T7) match7, - R Function(T8) match8, - R Function(T9) match9, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - case 3: - return match4(_value4 as T4); - case 4: - return match5(_value5 as T5); - case 5: - return match6(_value6 as T6); - case 6: - return match7(_value7 as T7); - case 7: - return match8(_value8 as T8); - default: - return match9(_value9 as T9); - } - } - - @override - int get hashCode => Object.hash( - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - _value8, - _value9, - ); - - @override - bool operator ==(Object other) => - other is Coproduct9 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4 && - other._value5 == _value5 && - other._value6 == _value6 && - other._value7 == _value7 && - other._value8 == _value8 && - other._value9 == _value9; - - @override - String toString() => [ - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - _value8, - _value9, - ].toCoproductString(); -} - -class Coproduct10 { - const Coproduct10.item1(T1 value) - : this._(value, null, null, null, null, null, null, null, null, null, 0); - - const Coproduct10.item2(T2 value) - : this._(null, value, null, null, null, null, null, null, null, null, 1); - - const Coproduct10.item3(T3 value) - : this._(null, null, value, null, null, null, null, null, null, null, 2); - - const Coproduct10.item4(T4 value) - : this._(null, null, null, value, null, null, null, null, null, null, 3); - - const Coproduct10.item5(T5 value) - : this._(null, null, null, null, value, null, null, null, null, null, 4); - - const Coproduct10.item6(T6 value) - : this._(null, null, null, null, null, value, null, null, null, null, 5); - - const Coproduct10.item7(T7 value) - : this._(null, null, null, null, null, null, value, null, null, null, 6); - - const Coproduct10.item8(T8 value) - : this._(null, null, null, null, null, null, null, value, null, null, 7); - - const Coproduct10.item9(T9 value) - : this._(null, null, null, null, null, null, null, null, value, null, 8); - - const Coproduct10.item10(T10 value) - : this._(null, null, null, null, null, null, null, null, null, value, 9); - - const Coproduct10._( - this._value1, - this._value2, - this._value3, - this._value4, - this._value5, - this._value6, - this._value7, - this._value8, - this._value9, - this._value10, - this._discriminator, - ); - - final T1? _value1; - final T2? _value2; - final T3? _value3; - final T4? _value4; - final T5? _value5; - final T6? _value6; - final T7? _value7; - final T8? _value8; - final T9? _value9; - final T10? _value10; - - final int _discriminator; - - R fold( - R Function(T1) match1, - R Function(T2) match2, - R Function(T3) match3, - R Function(T4) match4, - R Function(T5) match5, - R Function(T6) match6, - R Function(T7) match7, - R Function(T8) match8, - R Function(T9) match9, - R Function(T10) match10, - ) { - switch (_discriminator) { - case 0: - return match1(_value1 as T1); - case 1: - return match2(_value2 as T2); - case 2: - return match3(_value3 as T3); - case 3: - return match4(_value4 as T4); - case 4: - return match5(_value5 as T5); - case 5: - return match6(_value6 as T6); - case 6: - return match7(_value7 as T7); - case 7: - return match8(_value8 as T8); - case 8: - return match9(_value9 as T9); - default: - return match10(_value10 as T10); - } - } - - @override - int get hashCode => Object.hash( - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - _value8, - _value9, - _value10, - ); - - @override - bool operator ==(Object other) => - other is Coproduct10 && - other._value1 == _value1 && - other._value2 == _value2 && - other._value3 == _value3 && - other._value4 == _value4 && - other._value5 == _value5 && - other._value6 == _value6 && - other._value7 == _value7 && - other._value8 == _value8 && - other._value9 == _value9 && - other._value10 == _value10; - - @override - String toString() => [ - _value1, - _value2, - _value3, - _value4, - _value5, - _value6, - _value7, - _value8, - _value9, - _value10, - ].toCoproductString(); -} - -extension on Iterable { - String toCoproductString() => - '(${firstWhere((e) => e != null, orElse: () => null)?.toString() ?? ''})'; -} diff --git a/lib/src/either/either.dart b/lib/src/either/either.dart index 521086a..ade41a3 100644 --- a/lib/src/either/either.dart +++ b/lib/src/either/either.dart @@ -1,32 +1,24 @@ -// ignore_for_file: avoid_implementing_value_types - -import 'package:dfunc/dfunc.dart'; - -/// One of the popular examples of [Coproduct2] type. -/// -/// It represents a value that can be either of type [L] or of type [R]. -/// Usually [L] is assumed to be of Error type and [R] of Right type, e.g. -/// in pseudo-code: +/// Represents a value that can be either of type [L] or of type [R]. +/// Usually [L] is assumed to be of Error type and [R] of Right type, e.g.: /// /// ``` -/// type Result = Either +/// typedef Result = Either; /// ``` -abstract class Either implements Coproduct2 { +sealed class Either { const Either._(); - const factory Either.left(L value) = _Left._; + const factory Either.left(L value) = Left.new; - const factory Either.right(R value) = _Right._; + const factory Either.right(R value) = Right.new; - bool isLeft() => this is _Left; + bool isLeft() => this is Left; - bool isRight() => this is _Right; + bool isRight() => this is Right; L get _left; R get _right; - @override T fold(T Function(L) onLeft, T Function(R) onRight); Either map(T Function(R) f); @@ -35,14 +27,14 @@ abstract class Either implements Coproduct2 { Either mapLeft(L1 Function(L) f) => fold( (l) => Either.left(f(l)), - (r) => Either.right(r), + Either.right, ); - Either> combine(Either other) => isLeft() + Either combine(Either other) => isLeft() ? Either.left(_left) : other.isLeft() ? Either.left(other._left) - : Either.right(Product2(_right, other._right)); + : Either.right((_right, other._right)); } /// Sortcut for [Either.left]. @@ -51,64 +43,63 @@ Either left(L value) => Either.left(value); /// Shortcut for [Either.right]. Either right(R value) => Either.right(value); -class _Left extends Either { - const _Left._(this._value) : super._(); +final class Left extends Either { + const Left(this.value) : super._(); - final L _value; + final L value; @override - L get _left => _value; + L get _left => value; @override R get _right => throw StateError('right called on left value'); @override - T fold(T Function(L) onLeft, T Function(R) onRight) => onLeft(_value); + T fold(T Function(L) onLeft, T Function(R) onRight) => onLeft(value); @override - Either map(T Function(R) f) => _Left._(_value); + Either map(T Function(R) f) => Left(value); @override - Either flatMap(Either Function(R) f) => _Left._(_value); + Either flatMap(Either Function(R) f) => Left(value); @override - int get hashCode => _value.hashCode; + int get hashCode => value.hashCode; @override - bool operator ==(Object other) => - other is _Left && other._value == _value; + bool operator ==(Object other) => other is Left && other.value == value; @override - String toString() => 'Left($_value)'; + String toString() => 'Left($value)'; } -class _Right extends Either { - const _Right._(this._value) : super._(); +final class Right extends Either { + const Right(this.value) : super._(); - final R _value; + final R value; @override L get _left => throw StateError('left called on right value'); @override - R get _right => _value; + R get _right => value; @override - T fold(T Function(L) onLeft, T Function(R) onRight) => onRight(_value); + T fold(T Function(L) onLeft, T Function(R) onRight) => onRight(value); @override - Either map(T Function(R) f) => _Right._(f(_value)); + Either map(T Function(R) f) => Right(f(value)); @override - Either flatMap(Either Function(R) f) => f(_value); + Either flatMap(Either Function(R) f) => f(value); @override - int get hashCode => _value.hashCode; + int get hashCode => value.hashCode; @override bool operator ==(Object other) => - other is _Right && other._value == _value; + other is Right && other.value == value; @override - String toString() => 'Right($_value)'; + String toString() => 'Right($value)'; } diff --git a/lib/src/either/eithers.dart b/lib/src/either/eithers.dart index 5b1ccb3..0f10cf8 100644 --- a/lib/src/either/eithers.dart +++ b/lib/src/either/eithers.dart @@ -1,55 +1,50 @@ import 'package:dfunc/src/either/either.dart'; -import 'package:dfunc/src/product/product.dart'; abstract class Eithers { Eithers._(); - static Either> combine2( + static Either combine2( Either first, Either second, ) => first.combine(second); - static Either> combine3( + static Either combine3( Either first, Either second, Either third, ) => - first - .combine(second) - .combine(third) - .map((p) => Product3(p.item1.item1, p.item1.item2, p.item2)); + first.combine(second).combine(third).map((p) => (p.$1.$1, p.$1.$2, p.$2)); - static Either> combine4( + static Either combine4( Either first, Either second, Either third, Either fourth, ) => combine3(first, second, third).combine(fourth).map( - (p) => Product4( - p.item1.item1, - p.item1.item2, - p.item1.item3, - p.item2, + (p) => ( + p.$1.$1, + p.$1.$2, + p.$1.$3, + p.$2, ), ); - static Either> - combine5( + static Either combine5( Either first, Either second, Either third, Either fourth, Either fifth, ) => - combine4(first, second, third, fourth).combine(fifth).map( - (p) => Product5( - p.item1.item1, - p.item1.item2, - p.item1.item3, - p.item1.item4, - p.item2, - ), - ); + combine4(first, second, third, fourth).combine(fifth).map( + (p) => ( + p.$1.$1, + p.$1.$2, + p.$1.$3, + p.$1.$4, + p.$2, + ), + ); } diff --git a/lib/src/product/product.dart b/lib/src/product/product.dart deleted file mode 100644 index 02136de..0000000 --- a/lib/src/product/product.dart +++ /dev/null @@ -1,127 +0,0 @@ -class Product0 { - const Product0(); - - @override - int get hashCode => 0; - - @override - bool operator ==(Object other) => other is Product0; - - @override - String toString() => [].toProductString(); -} - -class Product1 { - const Product1(this.item1); - - final T1 item1; - - @override - int get hashCode => item1.hashCode; - - @override - bool operator ==(Object other) => other is Product1 && other.item1 == item1; - - @override - String toString() => [item1].toProductString(); -} - -class Product2 { - const Product2(this.item1, this.item2); - - final T1 item1; - - final T2 item2; - - @override - int get hashCode => Object.hash(item1, item2); - - @override - bool operator ==(Object other) => - other is Product2 && other.item1 == item1 && other.item2 == item2; - - @override - String toString() => [item1, item2].toProductString(); -} - -class Product3 { - const Product3(this.item1, this.item2, this.item3); - - final T1 item1; - - final T2 item2; - - final T3 item3; - - @override - int get hashCode => Object.hash(item1, item2, item3); - - @override - bool operator ==(Object other) => - other is Product3 && - other.item1 == item1 && - other.item2 == item2 && - other.item3 == item3; - - @override - String toString() => [item1, item2, item3].toProductString(); -} - -class Product4 { - const Product4(this.item1, this.item2, this.item3, this.item4); - - final T1 item1; - - final T2 item2; - - final T3 item3; - - final T4 item4; - - @override - int get hashCode => Object.hash(item1, item2, item3, item4); - - @override - bool operator ==(Object other) => - other is Product4 && - other.item1 == item1 && - other.item2 == item2 && - other.item3 == item3 && - other.item4 == item4; - - @override - String toString() => [item1, item2, item3, item4].toProductString(); -} - -class Product5 { - const Product5(this.item1, this.item2, this.item3, this.item4, this.item5); - - final T1 item1; - - final T2 item2; - - final T3 item3; - - final T4 item4; - - final T5 item5; - - @override - int get hashCode => Object.hash(item1, item2, item3, item4, item5); - - @override - bool operator ==(Object other) => - other is Product5 && - other.item1 == item1 && - other.item2 == item2 && - other.item3 == item3 && - other.item4 == item4 && - other.item5 == item5; - - @override - String toString() => [item1, item2, item3, item4, item5].toProductString(); -} - -extension on Iterable { - String toProductString() => '(${map((e) => e.toString()).join(', ')})'; -} diff --git a/lib/src/state.dart b/lib/src/state.dart index b9c9c06..c6313f8 100644 --- a/lib/src/state.dart +++ b/lib/src/state.dart @@ -1,12 +1,11 @@ import 'package:dfunc/src/func.dart'; -import 'package:dfunc/src/product/product.dart'; -typedef State = Product2 Function(S); +typedef State = (A, S) Function(S); abstract class States { States._(); - static State unit(A a) => (s) => Product2(a, s); + static State unit(A a) => (s) => (a, s); static State map2( State sa, @@ -15,9 +14,9 @@ abstract class States { ) => (s1) { final s2 = sa(s1); - final s3 = sb(s2.item2); + final s3 = sb(s2.$2); - return Product2(f(s2.item1, s3.item1), s3.item2); + return (f(s2.$1, s3.$1), s3.$2); }; } @@ -25,13 +24,13 @@ extension StateExt on State { State map(Func1 f) => (s1) { final s2 = this(s1); - return Product2(f(s2.item1), s2.item2); + return (f(s2.$1), s2.$2); }; State flatMap(Func1> f) => (s1) { final s2 = this(s1); - return f(s2.item1)(s2.item2); + return f(s2.$1)(s2.$2); }; } @@ -41,11 +40,11 @@ extension StateExtIterable on Iterable> { var x = s; for (final a in this) { final s2 = a(x); - r.add(s2.item1); - x = s2.item2; + r.add(s2.$1); + x = s2.$2; } - return Product2(r, x); + return (r, x); }; } diff --git a/lib/src/zip.dart b/lib/src/zip.dart index 9146f24..811b3ef 100644 --- a/lib/src/zip.dart +++ b/lib/src/zip.dart @@ -1,13 +1,13 @@ -import 'package:dfunc/dfunc.dart'; +import 'dart:core'; /// Creates an iterable of [Product2] by pairing up equally positioned /// items from both iterables [a] and [b]. The returned iterable is truncated to /// the length of the shorter of the two iterables. -Iterable> zip2(Iterable a, Iterable b) sync* { +Iterable<(A, B)> zip2(Iterable a, Iterable b) sync* { final first = a.iterator; final second = b.iterator; while (first.moveNext() && second.moveNext()) { - yield Product2(first.current, second.current); + yield (first.current, second.current); } } @@ -15,5 +15,5 @@ extension Zip on Iterable { /// Creates an iterable of [Product2] by pairing up equally positioned /// items from both iterables `this` and [other]. The returned iterable is /// truncated to the length of the shorter of the two iterables. - Iterable> zipWith(Iterable other) => zip2(this, other); + Iterable<(A, B)> zipWith(Iterable other) => zip2(this, other); } diff --git a/pubspec.yaml b/pubspec.yaml index aaedc93..d8f13e7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 0.8.2 homepage: https://github.com/ookami-kb/dfunc environment: - sdk: ">=2.14.0 <3.0.0" + sdk: ">=3.0.0 <4.0.0" dev_dependencies: melos: ^3.0.1 diff --git a/test/coproduct/coproduct_test.dart b/test/coproduct/coproduct_test.dart deleted file mode 100644 index 375ad85..0000000 --- a/test/coproduct/coproduct_test.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:dfunc/dfunc.dart'; -import 'package:test/test.dart'; - -void main() { - test('coproduct works', () { - const union = Coproduct2.item1('test'); - final result = union.fold((v) => v.toUpperCase(), (_) => 'error'); - expect(result, 'TEST'); - }); - - test('same coproducts are equal', () { - const union1 = Coproduct2.item1('test'); - const union2 = Coproduct2.item1('test'); - expect(union1 == union2, true); - }); - - test('different coproducts are not equal', () { - const union1 = Coproduct2.item1('test'); - const union2 = Coproduct2.item2(1); - expect(union1 == union2, false); - }); -} diff --git a/test/either/either_test.dart b/test/either/either_test.dart index c26a080..917fbfe 100644 --- a/test/either/either_test.dart +++ b/test/either/either_test.dart @@ -259,7 +259,7 @@ void main() { const e2 = Either.right(1); final result = e1.combine(e2); - expect(result.fold(always(null), identity), const Product2(1, 1)); + expect(result.fold(always(null), identity), const (1, 1)); }); }); diff --git a/test/either/eithers_test.dart b/test/either/eithers_test.dart index b28fd5f..f514ff8 100644 --- a/test/either/eithers_test.dart +++ b/test/either/eithers_test.dart @@ -9,7 +9,7 @@ void main() { const e4 = Either.right(4); const e5 = Either.right(5); final result = Eithers.combine5(e1, e2, e3, e4, e5); - expect(result.fold(always(null), identity), const Product5(1, 2, 3, 4, 5)); + expect(result.fold(always(null), identity), const (1, 2, 3, 4, 5)); }); test('combines 5 eithers with 1 left correctly', () { diff --git a/test/zip_test.dart b/test/zip_test.dart index b99cc8a..b26e1cb 100644 --- a/test/zip_test.dart +++ b/test/zip_test.dart @@ -8,9 +8,9 @@ void main() { expect( first.zipWith(second), const [ - Product2(1, 'a'), - Product2(2, 'b'), - Product2(3, 'c'), + (1, 'a'), + (2, 'b'), + (3, 'c'), ], ); });