-
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
IO.Action
and IO.Actions
in IO monad
Implemented `IO` applicative methods to handle sequential actions (`Action`) and collections of actions (`Actions`). Updated console methods to use generic monadic operations, improving flexibility and alignment with the new `IO` monad changes.
- Loading branch information
Showing
5 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using LanguageExt.Traits; | ||
|
||
namespace LanguageExt.DSL; | ||
|
||
record IOAction<A, B, C>(K<IO, A> Fa, K<IO, B> Fb, Func<B, IO<C>> Next) : InvokeSyncIO<C> | ||
{ | ||
public override IO<D> Map<D>(Func<C, D> f) => | ||
new IOAction<A, B, D>(Fa, Fb, b => Next(b).Map(f)); | ||
|
||
public override IO<D> Bind<D>(Func<C, K<IO, D>> f) => | ||
new IOAction<A, B, D>(Fa, Fb, b => Next(b).Bind(f)); | ||
|
||
public override IO<C> Invoke(EnvIO envIO) | ||
{ | ||
var taskA = Fa.As().RunAsync(envIO); | ||
if (taskA.IsCompleted) | ||
{ | ||
return Fb.Bind(Next).As(); | ||
} | ||
else | ||
{ | ||
return new IOActionAsync<A, B, C>(taskA, Fb, Next); | ||
} | ||
} | ||
} | ||
|
||
record IOActionAsync<A, B, C>(ValueTask<A> Fa, K<IO, B> Fb, Func<B, IO<C>> Next) : InvokeAsyncIO<C> | ||
{ | ||
public override IO<D> Map<D>(Func<C, D> f) => | ||
new IOActionAsync<A, B, D>(Fa, Fb, b => Next(b).Map(f)); | ||
|
||
public override IO<D> Bind<D>(Func<C, K<IO, D>> f) => | ||
new IOActionAsync<A, B, D>(Fa, Fb, b => Next(b).Bind(f)); | ||
|
||
public override async ValueTask<IO<C>> Invoke(EnvIO envIO) | ||
{ | ||
await Fa; | ||
return Fb.Bind(Next).As(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using LanguageExt.Common; | ||
using LanguageExt.Traits; | ||
|
||
namespace LanguageExt.DSL; | ||
|
||
record IOActions<A, B>(Iterator<K<IO, A>> Fas, Func<A, IO<B>> Next) : InvokeSyncIO<B> | ||
{ | ||
public override IO<C> Map<C>(Func<B, C> f) => | ||
new IOActions<A, C>(Fas, x => Next(x).Map(f)); | ||
|
||
public override IO<C> Bind<C>(Func<B, K<IO, C>> f) => | ||
new IOActions<A, C>(Fas, x => Next(x).Bind(f)); | ||
|
||
public override IO<B> Invoke(EnvIO envIO) | ||
{ | ||
if (Fas.IsEmpty) | ||
{ | ||
return IO.fail<B>(Error.New("Actions is empty")); | ||
} | ||
else | ||
{ | ||
var head = Fas.Head; | ||
var task = head.RunAsync(envIO); | ||
if (task.IsCompleted) | ||
{ | ||
return new IOActionsSync<A, B>(task.Result, Fas.Tail, Next); | ||
} | ||
else | ||
{ | ||
return new IOActionsAsync<A, B>(task, Fas.Tail, Next); | ||
} | ||
} | ||
} | ||
} | ||
|
||
record IOActionsSync<A, B>(A Value, Iterator<K<IO, A>> Fas, Func<A, IO<B>> Next) : InvokeSyncIO<B> | ||
{ | ||
public override IO<C> Map<C>(Func<B, C> f) => | ||
new IOActionsSync<A, C>(Value, Fas, x => Next(x).Map(f)); | ||
|
||
public override IO<C> Bind<C>(Func<B, K<IO, C>> f) => | ||
new IOActionsSync<A, C>(Value, Fas, x => Next(x).Bind(f)); | ||
|
||
public override IO<B> Invoke(EnvIO envIO) | ||
{ | ||
if (Fas.IsEmpty) | ||
{ | ||
return Next(Value); | ||
} | ||
else | ||
{ | ||
return new IOActions<A, B>(Fas, Next); | ||
} | ||
} | ||
} | ||
|
||
record IOActionsAsync<A, B>(ValueTask<A> Value, Iterator<K<IO, A>> Fas, Func<A, IO<B>> Next) : InvokeAsyncIO<B> | ||
{ | ||
public override IO<C> Map<C>(Func<B, C> f) => | ||
new IOActionsAsync<A, C>(Value, Fas, x => Next(x).Map(f)); | ||
|
||
public override IO<C> Bind<C>(Func<B, K<IO, C>> f) => | ||
new IOActionsAsync<A, C>(Value, Fas, x => Next(x).Bind(f)); | ||
|
||
public override async ValueTask<IO<B>> Invoke(EnvIO envIO) | ||
{ | ||
var value = await Value; | ||
|
||
if (Fas.IsEmpty) | ||
{ | ||
return Next(value); | ||
} | ||
else | ||
{ | ||
return new IOActions<A, B>(Fas, Next); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters