Skip to content

Commit

Permalink
Update language ext to version 5.0.0-beta-24
Browse files Browse the repository at this point in the history
  • Loading branch information
rungwiroon committed Oct 13, 2024
1 parent 7aa1164 commit e8b86b0
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Update="FSharp.Core" Version="8.0.400" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq.Expressions;
using LanguageExt;
using LanguageExt.Traits;

using static LanguageExt.Prelude;

Expand All @@ -21,12 +22,41 @@ public static class QueryableExtensions
/// <returns>
/// An Eff monad that represents the asynchronous operation. The Eff monad wraps a <see cref="List{T}"/> that contains elements from the input sequence.
/// </returns>
public static Eff<List<T>> ToListAsyncEff<T>(
public static Eff<List<T>> ToListEff<T>(
this IQueryable<T> source, CancellationToken ct = default)
{
return liftEff(() => source.ToListAsync(ct));
}

/// <summary>
/// Asynchronously converts an IQueryable&lt;T&gt; into a list within a K monad.
/// </summary>
/// <typeparam name="RT">The runtime environment type that implements Readable&lt;RT, EnvIO&gt; and Monad&lt;RT&gt;.</typeparam>
/// <typeparam name="T">The type of elements in the IQueryable.</typeparam>
/// <param name="source">The IQueryable to be converted to a list.</param>
/// <returns>
/// A K&lt;RT, List&lt;T&gt;&gt; representing the asynchronous operation.
/// The K monad wraps the result, which is the list of elements.
/// </returns>
public static K<RT, List<T>> ToListEffRt<RT, T>(
this IQueryable<T> source)
where RT : Readable<RT, EnvIO>, Monad<RT>
{
return
from env in Readable.ask<RT, EnvIO>()
from io in liftIO(() => source.ToListAsync(env.Token))
select io;
}

// public static ReaderT<RT, IO, List<T>> ToListRT2<RT, T>()
// where RT : Readable<RT, QueryableEnv<T>>, Monad<RT>
// {
// return
// from env in Readable.ask<RT, QueryableEnv<T>>()
// from io in Eff<QueryableEnv<T>, List<T>>.Lift(env => env.Queryable.ToListAsync(env.CancellationToken))
// select io;
// }

/// <summary>
/// Asynchronously converts a sequence to an array within an Eff monad.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="8.0.400" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="Marten" Version="7.26.3" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="MediatR" Version="12.4.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class PublisherExtensions
/// <param name="publisher">The publisher to use for publishing the notification.</param>
/// <param name="notification">The notification to publish.</param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>An <see cref="Eff{Unit}"/> representing the asynchronous operation.</returns>
/// <returns>An <see cref="LanguageExt.Eff{Unit}"/> representing the asynchronous operation.</returns>
public static Eff<LanguageExt.Unit> PublishEff<TNotification>(
this IPublisher publisher,
TNotification notification,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class SenderExtensions
/// <param name="sender">The sender to use for sending the command.</param>
/// <param name="command">The command to send.</param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>An <see cref="Eff{TCommandResult}"/> representing the asynchronous operation.</returns>
/// <returns>An <see cref="LanguageExt.Eff{TCommandResult}"/> representing the asynchronous operation.</returns>
public static Eff<TCommandResult> SendCommandEff<TCommand, TCommandResult>(
this ISender sender,
TCommand command,
Expand All @@ -40,7 +40,7 @@ await sender.Send(command, cancellationToken)
/// <param name="sender">The sender to use for sending the query.</param>
/// <param name="query">The query to send.</param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>An <see cref="Eff{TQueryResult}"/> representing the asynchronous operation.</returns>
/// <returns>An <see cref="LanguageExt.Eff{TQueryResult}"/> representing the asynchronous operation.</returns>
public static Eff<TQueryResult> SendQueryEff<TQuery, TQueryResult>(
this ISender sender,
TQuery query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-22" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
</ItemGroup>

<ItemGroup>
Expand Down
104 changes: 55 additions & 49 deletions src/Codehard.Functional/Codehard.Functional/EffEitherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,59 +43,68 @@ public static Eff<Either<TLeft, T>> GuardEither<T, TLeft>(
}

public static Eff<Either<TLeft, T>> GuardEitherAsync<T, TLeft>(
this Eff<Either<TLeft, T>> eitherValueAff,
this Eff<Either<TLeft, T>> eitherValueEff,
Func<T, ValueTask<bool>> predicateAsync,
Func<T, TLeft> leftF)
{
return
from eitherValue in eitherValueAff
from res in
liftEff(() =>
eitherValue.BindAsync(async val =>
await predicateAsync(val)
? Right<TLeft, T>(val)
: Left<TLeft, T>(leftF(val))))
select res;
eitherValueEff
.Bind(eitherValue =>
(from eitherValue2 in EitherT.lift<TLeft, Eff, T>(eitherValue)
from flag in liftEff(async () => await predicateAsync(eitherValue2))
from b in
flag
? Right<TLeft, T>(eitherValue2)
: Left<TLeft, T>(leftF(eitherValue2))
select b)
.Run()
);
}

public static Eff<Either<TLeft, T>> GuardEitherAsync<T, TLeft>(
this Eff<Either<TLeft, T>> eitherValueAff,
this Eff<Either<TLeft, T>> eitherValueEff,
Func<T, bool> predicate,
Func<T, ValueTask<TLeft>> leftAsync)
{
return
from eitherValue in eitherValueAff
from res in
liftEff(() =>
eitherValue.BindAsync(async val =>
predicate(val)
? Right<TLeft, T>(val)
: Left<TLeft, T>(await leftAsync(val))))
select res;
eitherValueEff
.Bind(eitherValue =>
(from eitherValue2 in EitherT.lift<TLeft, Eff, T>(eitherValue)
from flag in liftEff(() => predicate(eitherValue2))
from b in
flag
? SuccessEff(Right<TLeft, T>(eitherValue2))
: liftEff(async () => Left<TLeft, T>(await leftAsync(eitherValue2)))
select b)
.Run().Map(x => x.Flatten<TLeft, T>())
);
}

public static Eff<Either<TLeft, T>> GuardEitherAsync<T, TLeft>(
this Eff<Either<TLeft, T>> eitherValueAff,
this Eff<Either<TLeft, T>> eitherValueEff,
Func<T, ValueTask<bool>> predicateAsync,
Func<T, ValueTask<TLeft>> leftAsync)
{
return
from eitherValue in eitherValueAff
from res in
liftEff(() =>
eitherValue.BindAsync(async val =>
await predicateAsync(val)
? Right<TLeft, T>(val)
: Left<TLeft, T>(await leftAsync(val))))
select res;
eitherValueEff
.Bind(eitherValue =>
(from eitherValue2 in EitherT.lift<TLeft, Eff, T>(eitherValue)
from flag in liftEff(async () => await predicateAsync(eitherValue2))
from b in
flag
? SuccessEff(Right<TLeft, T>(eitherValue2))
: liftEff(async () => Left<TLeft, T>(await leftAsync(eitherValue2)))
select b)
.Run().Map(x => x.Flatten<TLeft, T>())
);
}

public static Eff<Either<TLeft, TRight>> MapRight<T, TRight, TLeft>(
this Eff<Either<TLeft, T>> eitherValueAff,
this Eff<Either<TLeft, T>> eitherValueEff,
Func<T, TRight> mapF)
{
return
eitherValueAff
eitherValueEff
.Map(eitherValue =>
eitherValue.Map(mapF));
}
Expand All @@ -105,39 +114,36 @@ public static Eff<Either<TLeft, TRight>> MapRightAsync<T, TRight, TLeft>(
Func<T, ValueTask<TRight>> mapAsync)
{
return
from eitherValue in eitherValueEff
from res in
liftEff(() =>
eitherValue
.MapAsync(async val =>
await mapAsync(val)))
select res;
eitherValueEff
.Bind(eitherValue =>
(from eitherValue2 in EitherT.lift<TLeft, Eff, T>(eitherValue)
from b in liftEff(async () => await mapAsync(eitherValue2))
select b)
.Run()
);
}

public static Eff<Either<TLeft, TRight>> DoIfRight<TRight, TLeft>(
this Eff<Either<TLeft, TRight>> eitherValueAff,
this Eff<Either<TLeft, TRight>> eitherValueEff,
Action<TRight> f)
{
return
eitherValueAff
eitherValueEff
.Map(eitherValue =>
eitherValue.Do(f));
}

public static Eff<Either<TLeft, TRight>> DoIfRightAsync<TRight, TLeft>(
this Eff<Either<TLeft, TRight>> eitherValueAff,
this Eff<Either<TLeft, TRight>> eitherValueEff,
Func<TRight, ValueTask<Unit>> f)
{
return
from eitherValue in eitherValueAff
from res in
liftEff(() =>
eitherValue
.MapAsync(async val =>
{
await f(val);
return val;
}))
select res;
eitherValueEff
.Bind(eitherValue =>
(from eitherValue2 in EitherT.lift<TLeft, Eff, TRight>(eitherValue)
from b in liftEff(async () => await f(eitherValue2))
select eitherValue2)
.Run()
);
}
}

0 comments on commit e8b86b0

Please sign in to comment.