Skip to content

Commit 8f48d2f

Browse files
Alt fixes #21
1 parent 863e9d4 commit 8f48d2f

3 files changed

Lines changed: 9 additions & 11 deletions

File tree

lib/src/io_either.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class IOEither<L, R> extends HKT2<_IOEitherHKT, L, R>
101101
/// Used to provide an **alt**ernative [IOEither] in case the current one returns [Left].
102102
@override
103103
IOEither<L, R> alt(covariant IOEither<L, R> Function() orElse) =>
104-
IOEither(() => run().match((_) => orElse().run(), (_) => run()));
104+
IOEither(() => run().match((_) => orElse().run(), right));
105105

106106
/// Chain multiple functions having the same left type `L`.
107107
@override

lib/src/task_either.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TaskEither<L, R> extends HKT2<_TaskEitherHKT, L, R>
2424
TaskEither<L, C> flatMap<C>(covariant TaskEither<L, C> Function(R r) f) =>
2525
TaskEither(() => run().then(
2626
(either) async => either.match(
27-
(l) => Either.left(l),
27+
left,
2828
(r) => f(r).run(),
2929
),
3030
));
@@ -71,17 +71,16 @@ class TaskEither<L, R> extends HKT2<_TaskEitherHKT, L, R>
7171
flatMap((_) => chain);
7272

7373
/// Change this [TaskEither] from `TaskEither<L, R>` to `TaskEither<R, L>`.
74-
TaskEither<R, L> swap() => TaskEither(
75-
() async => (await run()).match((l) => Right(l), (r) => Left(r)));
74+
TaskEither<R, L> swap() =>
75+
TaskEither(() async => (await run()).match(right, left));
7676

7777
/// When this [TaskEither] returns [Right], then return the current [TaskEither].
7878
/// Otherwise return the result of `orElse`.
7979
///
8080
/// Used to provide an **alt**ernative [TaskEither] in case the current one returns [Left].
8181
@override
8282
TaskEither<L, R> alt(covariant TaskEither<L, R> Function() orElse) =>
83-
TaskEither(
84-
() async => (await run()).match((_) => orElse().run(), (_) => run()));
83+
TaskEither(() async => (await run()).match((_) => orElse().run(), right));
8584

8685
/// If `f` applied on this [TaskEither] as [Right] returns `true`, then return this [TaskEither].
8786
/// If it returns `false`, return the result of `onFalse` in a [Left].
@@ -135,7 +134,7 @@ class TaskEither<L, R> extends HKT2<_TaskEitherHKT, L, R>
135134

136135
/// Build a [TaskEither] that returns a [Left] containing the result of running `task`.
137136
factory TaskEither.leftTask(Task<L> task) =>
138-
TaskEither(() => task.run().then((l) => Either.left(l)));
137+
TaskEither(() => task.run().then(left));
139138

140139
/// Build a [TaskEither] that returns a [Right] containing the result of running `task`.
141140
///
@@ -161,8 +160,7 @@ class TaskEither<L, R> extends HKT2<_TaskEitherHKT, L, R>
161160
/// When `option` is [Some], then return [Right] when
162161
/// running [TaskEither]. Otherwise return `onNone`.
163162
factory TaskEither.fromOption(Option<R> option, L Function() onNone) =>
164-
TaskEither(
165-
() async => option.match((r) => Right(r), () => Left(onNone())));
163+
TaskEither(() async => option.match(right, () => Left(onNone())));
166164

167165
/// Build a [TaskEither] that returns `either`.
168166
factory TaskEither.fromEither(Either<L, R> either) =>

lib/src/task_option.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class TaskOption<R> extends HKT<_TaskOptionHKT, R>
7777
///
7878
/// Used to provide an **alt**ernative [TaskOption] in case the current one returns [None].
7979
@override
80-
TaskOption<R> alt(covariant TaskOption<R> Function() orElse) => TaskOption(
81-
() async => (await run()).match((_) => run(), () => orElse().run()));
80+
TaskOption<R> alt(covariant TaskOption<R> Function() orElse) =>
81+
TaskOption(() async => (await run()).match(some, () => orElse().run()));
8282

8383
/// When this [TaskOption] returns a [None] then return the result of `orElse`.
8484
/// Otherwise return this [TaskOption].

0 commit comments

Comments
 (0)