Skip to content

Commit

Permalink
Experimental idea: IsBetween with explicit Inclusive and Exlusive bou…
Browse files Browse the repository at this point in the history
…ndaries.
  • Loading branch information
FreeApophis committed Mar 1, 2024
1 parent 6bfe87a commit 600b998
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Funcky.Test/Extensions/NumberExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Funcky.Test.Extensions;

public class NumberExtensionsTest
{
[Fact]
public void Example()
{
var position = 12;

Assert.True(position.IsBetween<Including, Excluding>(20, 0));
}
}
13 changes: 13 additions & 0 deletions Funcky/Extensions/Excluding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Funcky.Extensions;

public class Excluding : IIntervalBoundary

Check warning on line 3 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 3 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 3 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 3 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

{
private Excluding(int number)
{
Value = number;
}

public int Value { get; }

Check warning on line 10 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 10 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 10 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 10 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)


public static implicit operator Excluding(int number) => new(number);

Check warning on line 12 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Symbol 'implicit operator Excluding' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 12 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Symbol 'implicit operator Excluding' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 12 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Symbol 'implicit operator Excluding' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 12 in Funcky/Extensions/Excluding.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

Symbol 'implicit operator Excluding' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
}
6 changes: 6 additions & 0 deletions Funcky/Extensions/IIntervalBoundary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Funcky.Extensions;

public interface IIntervalBoundary

Check warning on line 3 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 3 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 3 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 3 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

{
int Value { get; }

Check warning on line 5 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 5 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 5 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 5 in Funcky/Extensions/IIntervalBoundary.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

}
13 changes: 13 additions & 0 deletions Funcky/Extensions/Including.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Funcky.Extensions;

public class Including : IIntervalBoundary

Check warning on line 3 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 3 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 3 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 3 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

{
private Including(int number)
{
Value = number;
}

public int Value { get; }

Check warning on line 10 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 10 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 10 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 10 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)


public static implicit operator Including(int number) => new(number);

Check warning on line 12 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Symbol 'implicit operator Including' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 12 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Symbol 'implicit operator Including' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 12 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Symbol 'implicit operator Including' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)

Check failure on line 12 in Funcky/Extensions/Including.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

Symbol 'implicit operator Including' is not part of the declared API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
}
35 changes: 35 additions & 0 deletions Funcky/Extensions/NumberExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Funcky.Extensions;

public static class NumberExtensions

Check warning on line 3 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 3 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 3 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 3 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

{
public static bool IsBetween<TFrom, TTo>(this int number, TFrom from, TTo to)

Check warning on line 5 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Generate NuGet Packages

Check failure on line 5 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 5.0.0)

Check failure on line 5 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Trimming Test

Check failure on line 5 in Funcky/Extensions/NumberExtensions.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest, 6.0.1)

where TFrom : IIntervalBoundary
where TTo : IIntervalBoundary
=> from.Value < to.Value
? IsBetweenForward(number, from, to)
: IsBetweenBackward(number, from, to);

private static bool IsBetweenForward<TFrom, TTo>(int number, TFrom from, TTo to)
where TFrom : IIntervalBoundary
where TTo : IIntervalBoundary
=> (from, to) switch
{
(Including, Including) => from.Value <= number && number <= to.Value,
(Including, Excluding) => from.Value <= number && number < to.Value,
(Excluding, Including) => from.Value < number && number <= to.Value,
(Excluding, Excluding) => from.Value < number && number < to.Value,
_ => false,
};

private static bool IsBetweenBackward<TFrom, TTo>(int number, TFrom from, TTo to)
where TFrom : IIntervalBoundary
where TTo : IIntervalBoundary
=> (from, to) switch
{
(Including, Including) => from.Value >= number && number >= to.Value,
(Including, Excluding) => from.Value >= number && number > to.Value,
(Excluding, Including) => from.Value > number && number >= to.Value,
(Excluding, Excluding) => from.Value > number && number > to.Value,
_ => false,
};
}

0 comments on commit 600b998

Please sign in to comment.