Skip to content

Commit

Permalink
Remove the option of using non-memoisable enumerables with the Schedu…
Browse files Browse the repository at this point in the history
…le system

Also renamed fix to fixedInternal
  • Loading branch information
louthy committed Jun 7, 2022
1 parent dd39561 commit fd4adb5
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 39 deletions.
56 changes: 50 additions & 6 deletions LanguageExt.Core/Effects/Schedule/Schedule.Constructors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract partial record Schedule
/// </remarks>
/// <param name="durations">durations to apply</param>
[Pure]
public static Schedule TimeSeries(Durations durations) =>
public static Schedule TimeSeries(params Duration[] durations) =>
new SchItems(durations);

/// <summary>
Expand All @@ -36,8 +36,52 @@ public static Schedule TimeSeries(Durations durations) =>
/// </remarks>
/// <param name="durations">durations to apply</param>
[Pure]
public static Schedule TimeSeries(params Duration[] durations) =>
new SchItems(durations);
public static Schedule TimeSeries(Arr<Duration> durations) =>
new SchItems(durations.Value);

/// <summary>
/// `Schedule` constructor that recurs for the specified durations.
/// </summary>
/// <remarks>
/// This allows for any custom schedule to be created, its the same as `ToSchedule()`.
/// </remarks>
/// <param name="durations">durations to apply</param>
[Pure]
public static Schedule TimeSeries(Seq<Duration> durations) =>
new SchItems(durations.Value);

/// <summary>
/// `Schedule` constructor that recurs for the specified durations.
/// </summary>
/// <remarks>
/// This allows for any custom schedule to be created, its the same as `ToSchedule()`.
/// </remarks>
/// <param name="durations">durations to apply</param>
[Pure]
public static Schedule TimeSeries(Lst<Duration> durations) =>
new SchItems(durations.Value);

/// <summary>
/// `Schedule` constructor that recurs for the specified durations.
/// </summary>
/// <remarks>
/// This allows for any custom schedule to be created, its the same as `ToSchedule()`.
/// </remarks>
/// <param name="durations">durations to apply</param>
[Pure]
public static Schedule TimeSeries(Set<Duration> durations) =>
new SchItems(durations.Value);

/// <summary>
/// `Schedule` constructor that recurs for the specified durations.
/// </summary>
/// <remarks>
/// This allows for any custom schedule to be created, its the same as `ToSchedule()`.
/// </remarks>
/// <param name="durations">durations to apply</param>
[Pure]
public static Schedule TimeSeries(HashSet<Duration> durations) =>
new SchItems(durations.Value);

/// <summary>
/// `ScheduleTransformer` constructor which provides mapping capabilities for `Schedule` instances.
Expand Down Expand Up @@ -69,13 +113,13 @@ public static ScheduleTransformer Transform(Func<Schedule, Schedule> transform)
/// <summary>
/// A schedule transformer that will enforce the first retry has no delay.
/// </summary>
public static ScheduleTransformer NoDelayOnFirst =
public static readonly ScheduleTransformer NoDelayOnFirst =
Transform(s => s.Tail.Prepend(Duration.Zero));

/// <summary>
/// Repeats the schedule forever.
/// </summary>
public static ScheduleTransformer RepeatForever =
public static readonly ScheduleTransformer RepeatForever =
Transform(s => new SchRepeatForever(s));

/// <summary>
Expand Down Expand Up @@ -151,7 +195,7 @@ internal static Duration secondsToIntervalStart(DateTime startTime, DateTime cur
/// <param name="interval">schedule interval</param>
/// <param name="currentTimeFn">current time function</param>
[Pure]
public static Schedule fix(Duration interval, Func<DateTime>? currentTimeFn = null) =>
public static Schedule fixedInterval(Duration interval, Func<DateTime>? currentTimeFn = null) =>
new SchFixed(interval, currentTimeFn);

///<summary>
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Core/Effects/Schedule/Schedule.DSL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal record SchBind2(Schedule Schedule, Func<Duration, Schedule> BindF, Func
{
public override IEnumerable<Duration> Run() =>
Schedule.Run().Bind(x => BindF(x).Run().Map(y => Project(x, y)));
}
}

/// <summary>
/// Tail of sequence
Expand Down
46 changes: 38 additions & 8 deletions LanguageExt.Core/Effects/Schedule/Schedule.Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LanguageExt.Effects.Traits;

namespace LanguageExt;

public static class ScheduleExtensions
{
/// <summary>
/// Converts an `IEnumerable` of positive durations to a schedule.
/// Converts an `Seq` of positive durations to a schedule.
/// </summary>
/// <param name="enumerable">Enumeration of positive durations</param>
/// <returns>schedule</returns>
[Pure]
public static Schedule ToSchedule(this IEnumerable<Duration> enumerable) =>
public static Schedule ToSchedule(this Seq<Duration> enumerable) =>
Schedule.TimeSeries(enumerable);

/// <summary>
/// Converts an `Arr` of positive durations to a schedule.
/// </summary>
/// <param name="enumerable">Enumeration of positive durations</param>
/// <returns>schedule</returns>
[Pure]
public static Schedule ToSchedule(this Arr<Duration> enumerable) =>
Schedule.TimeSeries(enumerable);

/// <summary>
/// Converts an `Lst` of positive durations to a schedule.
/// </summary>
/// <param name="enumerable">Enumeration of positive durations</param>
/// <returns>schedule</returns>
[Pure]
public static Schedule ToSchedule(this Lst<Duration> enumerable) =>
Schedule.TimeSeries(enumerable);

/// <summary>
/// Converts an `Set` of positive durations to a schedule.
/// </summary>
/// <param name="enumerable">Enumeration of positive durations</param>
/// <returns>schedule</returns>
[Pure]
public static Schedule ToSchedule(this Set<Duration> enumerable) =>
Schedule.TimeSeries(enumerable);

/// <summary>
/// Converts an `HashSet` of positive durations to a schedule.
/// </summary>
/// <param name="enumerable">Enumeration of positive durations</param>
/// <returns>schedule</returns>
[Pure]
public static Schedule ToSchedule(this HashSet<Duration> enumerable) =>
Schedule.TimeSeries(enumerable);

/// <summary>
Expand Down
22 changes: 7 additions & 15 deletions LanguageExt.Core/Effects/Schedule/Schedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ public abstract partial record Schedule
[Pure]
public abstract IEnumerable<Duration> Run();

//[Pure]
//public IEnumerator<Duration> GetEnumerator() =>
// Ops.AsEnumerable().GetEnumerator();

//[Pure]
//IEnumerator IEnumerable.GetEnumerator() =>
// Ops.AsEnumerable().GetEnumerator();

/// <summary>
/// Intersection of two schedules. As long as they are both running it returns the max duration.
/// </summary>
Expand Down Expand Up @@ -165,7 +157,7 @@ public Schedule Prepend(Duration value) =>
/// <param name="f">Mapping function</param>
/// <returns>Mapped schedule</returns>
[Pure]
public Schedule Map(Func<Duration, Duration> f) =>
internal Schedule Map(Func<Duration, Duration> f) =>
new SchMap(this, f);

/// <summary>
Expand All @@ -174,7 +166,7 @@ public Schedule Map(Func<Duration, Duration> f) =>
/// <param name="f">Mapping function</param>
/// <returns>Mapped schedule</returns>
[Pure]
public Schedule Map(Func<Duration, int, Duration> f) =>
internal Schedule Map(Func<Duration, int, Duration> f) =>
new SchMapIndex(this, f);

/// <summary>
Expand All @@ -183,7 +175,7 @@ public Schedule Map(Func<Duration, int, Duration> f) =>
/// <param name="f">Mapping function</param>
/// <returns>Mapped schedule</returns>
[Pure]
public Schedule Select(Func<Duration, Duration> f) =>
internal Schedule Select(Func<Duration, Duration> f) =>
new SchMap(this, f);

/// <summary>
Expand All @@ -192,7 +184,7 @@ public Schedule Select(Func<Duration, Duration> f) =>
/// <param name="f">Mapping function</param>
/// <returns>Mapped schedule</returns>
[Pure]
public Schedule Select(Func<Duration, int, Duration> f) =>
internal Schedule Select(Func<Duration, int, Duration> f) =>
new SchMapIndex(this, f);

/// <summary>
Expand All @@ -201,7 +193,7 @@ public Schedule Select(Func<Duration, int, Duration> f) =>
/// <param name="f">Bind function</param>
/// <returns>Chained schedule</returns>
[Pure]
public Schedule Bind(Func<Duration, Schedule> f) =>
internal Schedule Bind(Func<Duration, Schedule> f) =>
new SchBind(this, f);

/// <summary>
Expand All @@ -210,7 +202,7 @@ public Schedule Bind(Func<Duration, Schedule> f) =>
/// <param name="f">Bind function</param>
/// <returns>Chained schedule</returns>
[Pure]
public Schedule SelectMany(Func<Duration, Schedule> f) =>
internal Schedule SelectMany(Func<Duration, Schedule> f) =>
new SchBind(this, f);

/// <summary>
Expand All @@ -221,7 +213,7 @@ public Schedule SelectMany(Func<Duration, Schedule> f) =>
/// <param name="project">Project function</param>
/// <returns>Chained schedule</returns>
[Pure]
public Schedule SelectMany(Func<Duration, Schedule> bind, Func<Duration, Duration, Duration> project) =>
internal Schedule SelectMany(Func<Duration, Schedule> bind, Func<Duration, Duration, Duration> project) =>
new SchBind2(this, bind, project);

[Pure]
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Core/Effects/Schedule/ScheduleTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal ScheduleTransformer(Func<Schedule, Schedule> map) =>
/// <param name="schedule">`Schedule` to run through the transformer</param>
/// <returns>`Schedule` that has been run through the transformer</returns>
public Schedule Apply(Schedule schedule) =>
Map?.Invoke(schedule) ?? throw new BottomException();
Map?.Invoke(schedule) ?? schedule;

/// <summary>
/// Compose the two transformers into one
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Core/Immutable Collections/HashSet/HashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace LanguageExt
public static readonly HashSet<A> Empty = new HashSet<A>(TrieSet<EqDefault<A>, A>.Empty);

readonly TrieSet<EqDefault<A>, A> value;
TrieSet<EqDefault<A>, A> Value => value ?? TrieSet<EqDefault<A>, A>.Empty;
internal TrieSet<EqDefault<A>, A> Value => value ?? TrieSet<EqDefault<A>, A>.Empty;

internal HashSet(TrieSet<EqDefault<A>, A> value)
{
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Core/Immutable Collections/Seq/Seq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ public int CompareTo<OrdA>(Seq<A> rhs) where OrdA : struct, Ord<A>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Seq<A> Strict() =>
new Seq<A>(Value.Strict());
new (Value.Strict());

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Tests/ScheduleTest/AffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace LanguageExt.Tests.ScheduleTest;

public static class AffTests
{
static Schedule TestSchedule() => Schedule.fix(1 * ms) | Schedule.NoDelayOnFirst | Schedule.recurs(5);
static Schedule TestSchedule() => Schedule.fixedInterval(1 * ms) | Schedule.NoDelayOnFirst | Schedule.recurs(5);

[Fact]
public static async Task BailBeforeScheduleTest1()
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Tests/ScheduleTest/EffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LanguageExt.Tests.ScheduleTest;

public static class EffTests
{
static Schedule TestSchedule() => Schedule.fix(1 * ms) | Schedule.NoDelayOnFirst | Schedule.recurs(5);
static Schedule TestSchedule() => Schedule.fixedInterval(1 * ms) | Schedule.NoDelayOnFirst | Schedule.recurs(5);

[Fact]
public static void BailBeforeScheduleTest1()
Expand Down
8 changes: 4 additions & 4 deletions LanguageExt.Tests/ScheduleTest/ScheduleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public static void FromDurationsTest()
public static void FromDurationsTest2()
{
var result = Schedule.TimeSeries(
Range(1, 5)
.Where(x => x % 2 == 0)
.Select<int, Duration>(x => x * seconds));
Seq(1, 2, 3, 4, 5)
.Filter(x => x % 2 == 0)
.Map<Duration>(x => x * seconds));
result
.Run()
.Should()
Expand Down Expand Up @@ -259,7 +259,7 @@ public static void UpToTest()
[Fact]
public static void FixedTest()
{
var results = Schedule.fix(5 * sec, FromDates(FromDurations(Seq<Duration>(
var results = Schedule.fixedInterval(5 * sec, FromDates(FromDurations(Seq<Duration>(
6 * sec,
1 * sec,
4 * sec
Expand Down

0 comments on commit fd4adb5

Please sign in to comment.