Skip to content

Commit

Permalink
Zip prelide & extensions + IO
Browse files Browse the repository at this point in the history
- Zip prelude functions for all applicatives
- Zip extension methods for all applicatives
- IO specific functions for Prelude and Module
  • Loading branch information
louthy committed Oct 24, 2024
1 parent 073f266 commit e9a9777
Show file tree
Hide file tree
Showing 30 changed files with 660 additions and 234 deletions.
10 changes: 5 additions & 5 deletions LanguageExt.Core/Concurrency/VectorClock/VectorClock.A.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public record VectorClock<A>(Seq<(A, long)> Entries)
public static readonly VectorClock<A> Empty = new(Seq<(A, long)>());

public virtual bool Equals(VectorClock<A>? rhs) =>
rhs is not null &&
GetHashCode() == rhs.GetHashCode() &&
Count == rhs.Count &&
Entries.Zip(rhs.Entries).ForAll(p => equals<OrdDefault<A>, A>(p.Left.Item1, p.Right.Item1)) &&
Entries.Zip(rhs.Entries).ForAll(p => p.Left.Item2 == p.Right.Item2);
rhs is not null &&
GetHashCode() == rhs.GetHashCode() &&
Count == rhs.Count &&
Entries.Zip(rhs.Entries).ForAll(p => equals<OrdDefault<A>, A>(p.First.Item1, p.Second.Item1)) &&
Entries.Zip(rhs.Entries).ForAll(p => p.First.Item2 == p.Second.Item2);

public override int GetHashCode() =>
Entries.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public record VectorClock<OrdA, NumB, A, B>(Seq<(A, B)> Entries)
public static readonly VectorClock<OrdA, NumB, A, B> Empty = new(Seq<(A, B)>());

public virtual bool Equals(VectorClock<OrdA, NumB, A, B>? rhs) =>
rhs is not null &&
GetHashCode() == rhs.GetHashCode() &&
Count == rhs.Count &&
Entries.Zip(rhs.Entries).ForAll(p => equals<OrdA, A>(p.Left.Item1, p.Right.Item1)) &&
Entries.Zip(rhs.Entries).ForAll(p => equals<NumB, B>(p.Left.Item2, p.Right.Item2));
rhs is not null &&
GetHashCode() == rhs.GetHashCode() &&
Count == rhs.Count &&
Entries.Zip(rhs.Entries).ForAll(p => equals<OrdA, A>(p.First.Item1, p.Second.Item1)) &&
Entries.Zip(rhs.Entries).ForAll(p => equals<NumB, B>(p.First.Item2, p.Second.Item2));

public override int GetHashCode() =>
Entries.GetHashCode();
Expand Down
3 changes: 3 additions & 0 deletions LanguageExt.Core/Effects/IO/Async/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace LanguageExt;
/// <typeparam name="A">Bound value</typeparam>
record IOAsync<A>(Func<EnvIO, Task<IOResponse<A>>> runIO) : IO<A>
{
internal override bool IsAsync =>
true;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Functor
Expand Down
3 changes: 3 additions & 0 deletions LanguageExt.Core/Effects/IO/Fail/IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace LanguageExt;
/// <typeparam name="A">Bound value</typeparam>
record IOFail<A>(Error Error) : IO<A>
{
internal override bool IsAsync =>
false;

public IO<A> ToSync() =>
new IOSync<A>(_ => throw Error.ToErrorException());

Expand Down
36 changes: 31 additions & 5 deletions LanguageExt.Core/Effects/IO/IO.Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public static IO<Unit> lift(Action f) =>
public static K<M, A> local<M, A>(K<M, A> ma)
where M : Monad<M> =>
ma.LocalIO();

/// <summary>
/// Creates a local cancellation environment
/// </summary>
/// <remarks>
/// A local cancellation environment stops other IO computations, that rely on the same
/// environmental cancellation token, from being taken down by a regional cancellation.
///
/// If a `IO.cancel` is invoked locally then it will still create an exception that
/// propagates upwards and so catching cancellations is still important.
/// </remarks>
/// <param name="ma">Computation to run within the local context</param>
/// <typeparam name="A">Bound value</typeparam>
/// <returns>Result of the computation</returns>
public static IO<A> local<A>(K<IO, A> ma) =>
ma.As().Local();

public static IO<A> lift<A>(Either<Error, A> ma) =>
ma switch
Expand Down Expand Up @@ -107,7 +123,7 @@ public static IO<A> liftAsync<A>(Func<EnvIO, Task<A>> f) =>
public static IO<A> empty<A>() =>
IO<A>.Empty;

public static IO<A> or<A>(K<IO, A> ma, K<IO, A> mb) =>
public static IO<A> combine<A>(K<IO, A> ma, K<IO, A> mb) =>
ma.As() | mb.As();

/// <summary>
Expand Down Expand Up @@ -149,14 +165,24 @@ public static K<M, ForkIO<A>> fork<M, A>(K<M, A> ma, Option<TimeSpan> timeout =
mapIO(ma, mio => fork(mio , timeout));

/// <summary>
/// Yield the thread for the specified milliseconds or until cancelled.
/// Yield the thread for the specified duration or until cancelled.
/// </summary>
/// <param name="duration">Amount of time to yield for</param>
/// <returns>Unit</returns>
[Pure]
[MethodImpl(Opt.Default)]
public static IO<Unit> yieldFor(Duration duration) =>
IO<Unit>.LiftAsync(env => yieldFor(duration, env.Token));

/// <summary>
/// Yield the thread for the specified duration or until cancelled.
/// </summary>
/// <param name="milliseconds">Amount of time to yield for</param>
/// <param name="timeSpan">Amount of time to yield for</param>
/// <returns>Unit</returns>
[Pure]
[MethodImpl(Opt.Default)]
public static IO<Unit> yield(double milliseconds) =>
IO<Unit>.LiftAsync(env => yieldFor(new Duration(milliseconds), env.Token));
public static IO<Unit> yieldFor(TimeSpan timeSpan) =>
IO<Unit>.LiftAsync(env => yieldFor(timeSpan, env.Token));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Expand Down
Loading

0 comments on commit e9a9777

Please sign in to comment.