From 62a5d01fa43c4e26a044d23f24b024b9a39304d3 Mon Sep 17 00:00:00 2001 From: SandroMaglione Date: Sat, 23 Mar 2024 17:19:06 +0900 Subject: [PATCH] conversions and folding --- packages/fpdart/lib/src/effect.dart | 114 +++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 4 deletions(-) diff --git a/packages/fpdart/lib/src/effect.dart b/packages/fpdart/lib/src/effect.dart index f3ba871..e2a9079 100644 --- a/packages/fpdart/lib/src/effect.dart +++ b/packages/fpdart/lib/src/effect.dart @@ -185,7 +185,8 @@ final class Effect extends IEffect { ); /// {@category constructors} - factory Effect.fromNullable(R? value, L Function() onNull) => Effect._( + factory Effect.fromNullable(R? value, {required L Function() onNull}) => + Effect._( (_) => value == null ? Left(Fail(onNull())) : Right(value), ); @@ -217,9 +218,9 @@ final class Effect extends IEffect { ); /// {@category constructors} - static Effect unit = Effect._( - (_) => const Right(fpdart_unit.unit), - ); + static Effect unit() => Effect._( + (_) => const Right(fpdart_unit.unit), + ); /// {@category collecting} static Effect> forEach( @@ -300,6 +301,101 @@ final class Effect extends IEffect { ), ); + /// {@category conversions} + Effect> either() => Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => switch (cause) { + Fail(error: final error) => Right(Left(error)), + Die() => Left(cause), + }, + Right(value: final value) => Right(Right(value)), + }, + ), + ); + + /// {@category conversions} + Effect> option() => Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => switch (cause) { + Fail() => Right(None()), + Die() => Left(cause), + }, + Right(value: final value) => Right(Some(value)), + }, + ), + ); + + /// {@category conversions} + Effect> exit() => Effect._( + (env) => _unsafeRun(env).then( + (exit) => Right(exit), + ), + ); + + /// {@category folding} + Effect match({ + required C Function(L l) onFailure, + required C Function(R r) onSuccess, + }) => + Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => switch (cause) { + Fail(error: final error) => Right(onFailure(error)), + Die() => Left(cause), + }, + Right(value: final value) => Right(onSuccess(value)), + }, + ), + ); + + /// {@category folding} + Effect matchCause({ + required C Function(Cause l) onFailure, + required C Function(R r) onSuccess, + }) => + Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => Right(onFailure(cause)), + Right(value: final value) => Right(onSuccess(value)), + }, + ), + ); + + /// {@category folding} + Effect matchEffect({ + required Effect Function(L l) onFailure, + required Effect Function(R r) onSuccess, + }) => + Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => switch (cause) { + Fail(error: final error) => onFailure(error)._unsafeRun(env), + Die() => Left(cause), + }, + Right(value: final value) => onSuccess(value)._unsafeRun(env), + }, + ), + ); + + /// {@category folding} + Effect matchCauseEffect({ + required Effect Function(Cause l) onFailure, + required Effect Function(R r) onSuccess, + }) => + Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => onFailure(cause)._unsafeRun(env), + Right(value: final value) => onSuccess(value)._unsafeRun(env), + }, + ), + ); + /// {@category mapping} Effect flip() => Effect._( (env) => _unsafeRun(env).then( @@ -329,6 +425,16 @@ final class Effect extends IEffect { ), ); + /// {@category mapping} + Effect mapErrorCause(C Function(Cause l) f) => Effect._( + (env) => _unsafeRun(env).then( + (exit) => switch (exit) { + Left(value: final cause) => Left(Fail(f(cause))), + Right(value: final value) => Right(value), + }, + ), + ); + /// {@category mapping} Effect mapBoth(C Function(L l) fl, D Function(R r) fr) => Effect._(