Skip to content

Commit 55cf8b4

Browse files
author
Meerow
authored
feat: add more, less and exact for enumerables
feat: add more, less and exact for enumerables
2 parents af1f7bb + 66ea7c8 commit 55cf8b4

File tree

8 files changed

+259
-9
lines changed

8 files changed

+259
-9
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections;
3+
using Yaapii.Atoms.Scalar;
4+
5+
namespace Yaapii.Atoms.Enumerable
6+
{
7+
/// <summary>
8+
/// Tells if an enumerable has exactly the the specified item count.
9+
/// </summary>
10+
public sealed class ExactAmount : ScalarEnvelope<bool>
11+
{
12+
/// <summary>
13+
/// Tells if an enumerable has exactly the the specified item count.
14+
/// </summary>
15+
public ExactAmount(int amount, IEnumerable source) : base(() =>
16+
{
17+
if (amount < 0)
18+
{
19+
throw new ArgumentException($"A positive number is needed for amount (amount: {amount}).");
20+
}
21+
var current = 0;
22+
var enumerator = source.GetEnumerator();
23+
while (enumerator.MoveNext() && current <= amount)
24+
{
25+
current++;
26+
}
27+
return current == amount;
28+
})
29+
{ }
30+
}
31+
}

src/Yaapii.Atoms/Enumerable/LengthOf.cs

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ namespace Yaapii.Atoms.Enumerable
3434
/// </summary>
3535
public sealed class LengthOf : ScalarEnvelope<Int32>
3636
{
37-
private readonly ScalarOf<Int32> result;
38-
3937
/// <summary>
4038
/// Length of an <see cref="IEnumerable"/>
4139
/// </summary>
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections;
3+
using Yaapii.Atoms.Scalar;
4+
5+
namespace Yaapii.Atoms.Enumerable
6+
{
7+
/// <summary>
8+
/// Tells if an enumerable has less than the specified items.
9+
/// </summary>
10+
public sealed class LessThan : ScalarEnvelope<bool>
11+
{
12+
/// <summary>
13+
/// Tells if an enumerable has less than the specified items.
14+
/// </summary>
15+
public LessThan(int amount, IEnumerable source) : base(() =>
16+
{
17+
if (amount < 0)
18+
{
19+
throw new ArgumentException($"A positive number is needed for amount (amount: {amount})");
20+
}
21+
var current = 0;
22+
var enumerator = source.GetEnumerator();
23+
while (enumerator.MoveNext() && current <= amount)
24+
{
25+
current++;
26+
}
27+
return current < amount;
28+
})
29+
{ }
30+
}
31+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections;
3+
using Yaapii.Atoms.Scalar;
4+
5+
namespace Yaapii.Atoms.Enumerable
6+
{
7+
/// <summary>
8+
/// Tells if an enumerable has more than the specified items.
9+
/// </summary>
10+
public sealed class MoreThan : ScalarEnvelope<bool>
11+
{
12+
/// <summary>
13+
/// Tells if an enumerable has more than the specified items.
14+
/// </summary>
15+
public MoreThan(int amount, IEnumerable source) : base(() =>
16+
{
17+
if (amount < 0)
18+
{
19+
throw new ArgumentException($"A positive number is needed for amount (amount: {amount})");
20+
}
21+
var current = 0;
22+
var enumerator = source.GetEnumerator();
23+
while (enumerator.MoveNext() && current <= amount)
24+
{
25+
current++;
26+
}
27+
return current > amount;
28+
})
29+
{ }
30+
}
31+
}

src/Yaapii.Atoms/Scalar/ScalarOf.cs

+43-7
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,20 @@ public sealed class ScalarOf<T> : IScalar<T>
4141
/// <param name="src">func to cache result from</param>
4242
public ScalarOf(T src) : this(new Live<T>(src))
4343
{ }
44-
public static ScalarOf<T> New(T src) => new ScalarOf<T>(src);
4544

4645
/// <summary>
4746
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache always.
4847
/// </summary>
4948
/// <param name="src">func to cache result from</param>
5049
public ScalarOf(Func<T> src) : this(new Live<T>(src))
5150
{ }
52-
public static ScalarOf<T> New(Func<T> src) => new ScalarOf<T>(src);
5351

5452
/// <summary>
5553
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache always.
5654
/// </summary>
5755
/// <param name="src">scalar to cache result from</param>
5856
public ScalarOf(IScalar<T> src) : this(src, input => false)
5957
{ }
60-
public static ScalarOf<T> New(IScalar<T> src) => new ScalarOf<T>(src);
6158

6259
/// <summary>
6360
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache as long the reload condition is false.
@@ -66,7 +63,6 @@ public ScalarOf(IScalar<T> src) : this(src, input => false)
6663
/// <param name="shouldReload">reload condition func</param>
6764
public ScalarOf(Func<T> srcFunc, Func<T, bool> shouldReload) : this(new Live<T>(srcFunc), shouldReload)
6865
{ }
69-
public static ScalarOf<T> New(Func<T> srcFunc, Func<T, bool> shouldReload) => new ScalarOf<T>(srcFunc, shouldReload);
7066

7167
/// <summary>
7268
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache as long the reload condition is false.
@@ -75,7 +71,6 @@ public ScalarOf(Func<T> srcFunc, Func<T, bool> shouldReload) : this(new Live<T>(
7571
/// <param name="shouldReload">reload condition func</param>
7672
public ScalarOf(IFunc<T> srcFunc, Func<T, bool> shouldReload) : this(new Live<T>(srcFunc), shouldReload)
7773
{ }
78-
public static ScalarOf<T> New(IFunc<T> srcFunc, Func<T, bool> shouldReload) => new ScalarOf<T>(srcFunc, shouldReload);
7974

8075
/// <summary>
8176
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache as long the reload condition is false.
@@ -89,7 +84,6 @@ public ScalarOf(IScalar<T> src, Func<T, bool> shouldReload)
8984
this.cache = new T[1];
9085
this.filled = new bool[1];
9186
}
92-
public static ScalarOf<T> New(IScalar<T> src, Func<T, bool> shouldReload) => new ScalarOf<T>(src, shouldReload);
9387

9488
/// <summary>
9589
/// Get the value.
@@ -107,6 +101,48 @@ public T Value()
107101
this.cache[0] = this.origin.Value();
108102
}
109103
return this.cache[0];
110-
}
104+
}
105+
}
106+
107+
public static class ScalarOf
108+
{
109+
/// <summary>
110+
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache always.
111+
/// </summary>
112+
/// <param name="src">func to cache result from</param>
113+
public static ScalarOf<T> New<T>(T src) => new ScalarOf<T>(src);
114+
115+
/// <summary>
116+
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache always.
117+
/// </summary>
118+
/// <param name="src">func to cache result from</param>
119+
public static ScalarOf<T> New<T>(Func<T> src) => new ScalarOf<T>(src);
120+
121+
/// <summary>
122+
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache always.
123+
/// </summary>
124+
/// <param name="src">scalar to cache result from</param>
125+
public static ScalarOf<T> New<T>(IScalar<T> src) => new ScalarOf<T>(src);
126+
127+
/// <summary>
128+
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache as long the reload condition is false.
129+
/// </summary>
130+
/// <param name="srcFunc">func to cache result from</param>
131+
/// <param name="shouldReload">reload condition func</param>
132+
public static ScalarOf<T> New<T>(IFunc<T> srcFunc, Func<T, bool> shouldReload) => new ScalarOf<T>(srcFunc, shouldReload);
133+
134+
/// <summary>
135+
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache as long the reload condition is false.
136+
/// </summary>
137+
/// <param name="srcFunc">func to cache result from</param>
138+
/// <param name="shouldReload">reload condition func</param>
139+
public static ScalarOf<T> New<T>(Func<T> srcFunc, Func<T, bool> shouldReload) => new ScalarOf<T>(srcFunc, shouldReload);
140+
141+
/// <summary>
142+
/// A s<see cref="IScalar{T}"/> that will return the same value from a cache as long the reload condition is false.
143+
/// </summary>
144+
/// <param name="src">scalar to cache result from</param>
145+
/// <param name="shouldReload">reload condition func</param>
146+
public static ScalarOf<T> New<T>(IScalar<T> src, Func<T, bool> shouldReload) => new ScalarOf<T>(src, shouldReload);
111147
}
112148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Xunit;
2+
using Yaapii.Atoms.Enumerable;
3+
4+
namespace Yaapii.Atoms.Tests.Enumerable
5+
{
6+
public sealed class ExactAmountTests
7+
{
8+
[Fact]
9+
public void DetectsMatch()
10+
{
11+
Assert.True(
12+
new ExactAmount(
13+
3,
14+
new ManyOf("a", "b", "c")
15+
).Value()
16+
);
17+
}
18+
19+
[Fact]
20+
public void NoMatchOnLess()
21+
{
22+
Assert.False(
23+
new ExactAmount(
24+
3,
25+
new ManyOf("a", "b")
26+
).Value()
27+
);
28+
}
29+
30+
[Fact]
31+
public void NoMatchOnMore()
32+
{
33+
Assert.False(
34+
new ExactAmount(
35+
3,
36+
new ManyOf("a", "b", "c", "d")
37+
).Value()
38+
);
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Xunit;
2+
using Yaapii.Atoms.Enumerable;
3+
4+
namespace Yaapii.Atoms.Tests.Enumerable
5+
{
6+
public sealed class LessThanTests
7+
{
8+
[Fact]
9+
public void DetectsLess()
10+
{
11+
Assert.True(
12+
new LessThan(
13+
3,
14+
new ManyOf("a", "b")
15+
).Value()
16+
);
17+
}
18+
19+
[Fact]
20+
public void NoMatchOnMore()
21+
{
22+
Assert.False(
23+
new LessThan(
24+
3,
25+
new ManyOf("a", "b", "c", "d")
26+
).Value()
27+
);
28+
}
29+
30+
[Fact]
31+
public void NoMatchOnEqual()
32+
{
33+
Assert.False(
34+
new LessThan(
35+
3,
36+
new ManyOf("a", "b", "c")
37+
).Value()
38+
);
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Xunit;
2+
using Yaapii.Atoms.Enumerable;
3+
4+
namespace Yaapii.Atoms.Tests.Enumerable
5+
{
6+
public sealed class MoreThanTests
7+
{
8+
[Fact]
9+
public void DetectsMore()
10+
{
11+
Assert.True(
12+
new MoreThan(
13+
3,
14+
new ManyOf("a", "b", "c", "d")
15+
).Value()
16+
);
17+
}
18+
19+
[Fact]
20+
public void NoMatchOnLess()
21+
{
22+
Assert.False(
23+
new MoreThan(
24+
3,
25+
new ManyOf("a", "b")
26+
).Value()
27+
);
28+
}
29+
30+
[Fact]
31+
public void NoMatchOnEqual()
32+
{
33+
Assert.False(
34+
new MoreThan(
35+
3,
36+
new ManyOf("a", "b", "c")
37+
).Value()
38+
);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)