-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental idea: IsBetween with explicit Inclusive and Exlusive boundaries #783
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} | ||
} |
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
|
||
{ | ||
private Excluding(int number) | ||
{ | ||
Value = number; | ||
} | ||
|
||
public int Value { get; } | ||
Check warning on line 10 in Funcky/Extensions/Excluding.cs
|
||
|
||
public static implicit operator Excluding(int number) => new(number); | ||
Check warning on line 12 in Funcky/Extensions/Excluding.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
|
||
{ | ||
int Value { get; } | ||
Check warning on line 5 in Funcky/Extensions/IIntervalBoundary.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
|
||
{ | ||
private Including(int number) | ||
{ | ||
Value = number; | ||
} | ||
|
||
public int Value { get; } | ||
Check warning on line 10 in Funcky/Extensions/Including.cs
|
||
|
||
public static implicit operator Including(int number) => new(number); | ||
Check warning on line 12 in Funcky/Extensions/Including.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
|
||
{ | ||
public static bool IsBetween<TFrom, TTo>(this int number, TFrom from, TTo to) | ||
Check warning on line 5 in Funcky/Extensions/NumberExtensions.cs
|
||
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, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea:
InRange
instead ofIsBetween