Skip to content

Commit

Permalink
provide and mapEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 24, 2024
1 parent f02afe3 commit df5740d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
26 changes: 20 additions & 6 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
(_) => f().then(Right.new),
);

/// {@category constructors}
factory Effect.from(Exit<L, R> Function(E env) f) => Effect._(f);

/// {@category constructors}
factory Effect.fail(L value) => Effect._((_) => Left(Failure(value)));

Expand Down Expand Up @@ -282,13 +285,14 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
) =>
flatMap((_) => effect);

/// Extract the required dependency from the complete environment.
///
/// {@category do_notation}
Effect<V, L, R> provide<V>(E Function(V env) f) => Effect._(
Effect<V, L, R> mapEnv<V>(E Function(V env) f) => Effect._(
(env) => _unsafeRun(f(env)),
);

/// {@category do_notation}
Effect<Null, L, R> provide(E env) => Effect._((_) => _unsafeRun(env));

/// {@category do_notation}
static Effect<E, L, E> env<E, L>() => Effect._(
(env) => Right(env),
Expand Down Expand Up @@ -589,11 +593,21 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
);
}

extension ProvideVoid<L, R> on Effect<void, L, R> {
/// Add a required dependency instead of [void].
///
extension ProvideNull<L, R> on Effect<Null, L, R> {
/// {@category do_notation}
Effect<V, L, R> withEnv<V>() => Effect._(
(env) => _unsafeRun(null),
);

/// {@category execution}
R runSyncVoid() => runSync(null);

/// {@category execution}
Future<R> runFutureVoid() => runFuture(null);

/// {@category execution}
Either<Cause<L>, R> runSyncExitVoid() => runSyncExit(null);

/// {@category execution}
Future<Either<Cause<L>, R>> runFutureExitVoid() => runFutureExit(null);
}
8 changes: 4 additions & 4 deletions packages/fpdart/test/src/effect/effect_constructors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void main() {

group('gen', () {
test('sync succeed', () {
final main = Effect<void, Never, int>.gen(($) {
final main = Effect<Null, Never, int>.gen(($) {
final value = $.sync(Effect.succeed(10));
return value;
});
Expand All @@ -62,7 +62,7 @@ void main() {
});

test('sync fail', () {
final main = Effect<void, String, int>.gen(($) {
final main = Effect<Null, String, int>.gen(($) {
final value = $.sync(Effect.fail("abc"));
return value;
});
Expand All @@ -71,7 +71,7 @@ void main() {
});

test('async succeed', () async {
final main = Effect<void, Never, int>.gen(($) async {
final main = Effect<Null, Never, int>.gen(($) async {
final value =
await $.async(Effect.functionSucceed(() => Future.value(10)));
return value;
Expand All @@ -81,7 +81,7 @@ void main() {
});

test('fail when running async as sync', () async {
final main = Effect<void, Never, int>.gen(($) {
final main = Effect<Null, Never, int>.gen(($) {
final value = $.sync(Effect.functionSucceed(
() async => Future.value(10),
));
Expand Down
36 changes: 36 additions & 0 deletions packages/fpdart/test/src/effect/effect_do_notation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "package:fpdart/fpdart.dart";
import "package:test/test.dart";

void main() {
group(
"Effect do notation",
() {
group('provide', () {
test('remove dependency', () {
final main = Effect<String, String, int>.gen(($) {
final env = $.sync(Effect.env());
return env.length;
});

final program = main.provide("abc");
final result = program.runSyncVoid();
expect(result, 3);
});
});

group('mapEnv', () {
test('adapt dependency from another program', () {
final subMain =
Effect<int, String, int>.from((env) => Right(env + 1));
final main = Effect<String, String, int>.gen(($) {
final value = $.sync(subMain.mapEnv((env) => env.length));
return value;
});

final result = main.runSync("abc");
expect(result, 4);
});
});
},
);
}

0 comments on commit df5740d

Please sign in to comment.