Skip to content

Commit

Permalink
Solve map recursion issue
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Dec 19, 2024
1 parent cd4153e commit 03ac342
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
40 changes: 38 additions & 2 deletions LanguageExt.Core/Effects/IO/DSL/IOMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,53 @@ abstract record IOMapAsync<A> : IODsl<A>
record IOMap<A, B>(A Value, Func<A, B> F) : IOMap<B>
{
public override IODsl<C> Map<C>(Func<B, C> g) =>
new IOMap<A, C>(Value, x => g(F(x)));
new IOMap<A, B, C>(Value, F, g);

public override B Invoke(EnvIO envIO) =>
F(Value);
}

record IOMap<A, B, C>(A Value, Func<A, B> F, Func<B, C> G) : IOMap<C>
{
public override IODsl<D> Map<D>(Func<C, D> h) =>
new IOMap<A, B, C, D>(Value, F, G, h);

public override C Invoke(EnvIO envIO) =>
G(F(Value));
}

record IOMap<A, B, C, D>(A Value, Func<A, B> F, Func<B, C> G, Func<C, D> H) : IOMap<D>
{
public override IODsl<E> Map<E>(Func<D, E> f) =>
new IOMap<A, B, C, E>(Value, F, G, x => f(H(x)));

public override D Invoke(EnvIO envIO) =>
H(G(F(Value)));
}

record IOMapAsync<A, B>(Task<A> Value, Func<A, B> F) : IOMapAsync<B>
{
public override IODsl<C> Map<C>(Func<B, C> g) =>
new IOMapAsync<A, C>(Value, x => g(F(x)));
new IOMapAsync<A, B, C>(Value, F, g);

public override async ValueTask<B> Invoke(EnvIO envIO) =>
F(await Value);
}

record IOMapAsync<A, B, C>(Task<A> Value, Func<A, B> F, Func<B, C> G) : IOMapAsync<C>
{
public override IODsl<D> Map<D>(Func<C, D> h) =>
new IOMapAsync<A, B, C, D>(Value, F, G, h);

public override async ValueTask<C> Invoke(EnvIO envIO) =>
G(F(await Value));
}

record IOMapAsync<A, B, C, D>(Task<A> Value, Func<A, B> F, Func<B, C> G, Func<C, D> H) : IOMapAsync<D>
{
public override IODsl<E> Map<E>(Func<D, E> f) =>
new IOMapAsync<A, B, C, E>(Value, F, G, x => f(H(x)));

public override async ValueTask<D> Invoke(EnvIO envIO) =>
H(G(F(await Value)));
}
8 changes: 4 additions & 4 deletions LanguageExt.Core/Effects/IO/Free/IOBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ abstract record IOBindAsync<A> : IO<A>
record IOBind<A, B>(A Value, Func<A, K<IO, B>> F) : IOBind<B>
{
public override IO<C> Map<C>(Func<B, C> f) =>
new IOBind<A, C>(Value, x => F(x).Map(f));
new IOBind<A, C>(Value, x => F(x).As().Map(f));

public override IO<C> Bind<C>(Func<B, K<IO, C>> f) =>
new IOBind<A, C>(Value, x => F(x).Bind(f));
new IOBind<A, C>(Value, x => F(x).As().Bind(f));

public override IO<B> Invoke(EnvIO envIO) =>
F(Value).As();
Expand All @@ -30,10 +30,10 @@ public override IO<B> Invoke(EnvIO envIO) =>
record IOBindAsync<A, B>(Task<A> Value, Func<A, K<IO, B>> F) : IOBindAsync<B>
{
public override IO<C> Map<C>(Func<B, C> f) =>
new IOBindAsync<A, C>(Value, x => F(x).Map(f));
new IOBindAsync<A, C>(Value, x => F(x).As().Map(f));

public override IO<C> Bind<C>(Func<B, K<IO, C>> f) =>
new IOBindAsync<A, C>(Value, x => F(x).Bind(f));
new IOBindAsync<A, C>(Value, x => F(x).As().Bind(f));

public override async ValueTask<IO<B>> Invoke(EnvIO envIO) =>
F(await Value).As();
Expand Down

0 comments on commit 03ac342

Please sign in to comment.