made methods EnumerableExtensions.CountAtLeast/Most
This commit is contained in:
1 parent
a80b650dcc
commit
28f8d62a1a
2 files changed
+37
No files matched your search
@@ -47,6 +47,28 @@ namespace System.Collections.Generic {
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static bool CountAtLeast<T>(this IEnumerable<T> source, int count) {
|
||||
if (count < 0)
|
||||
throw new ArgumentException(ExceptionBuilder.Format.LessThan(0), nameof(count));
|
||||
if (count == 0)
|
||||
return true;
|
||||
foreach (var v in source) {
|
||||
if (--count == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool CountAtMost<T>(this IEnumerable<T> source, int count) {
|
||||
if (count < 0)
|
||||
throw new ArgumentException(ExceptionBuilder.Format.LessThan(0), nameof(count));
|
||||
foreach (var v in source) {
|
||||
if (--count == -1)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Convert<T>(this IEnumerable source) {
|
||||
foreach (var v in source)
|
||||
yield return ConvertUtils.try_convert<T>(v);
|
||||
|
||||
@@ -31,5 +31,20 @@ namespace mbts.utils {
|
||||
public static ArgumentOutOfRangeException gte_length(string name) =>
|
||||
new ArgumentOutOfRangeException(name, "Index is greater than or equal to the number of elements.");
|
||||
}
|
||||
|
||||
public static class Format {
|
||||
public static string LessThan(int value) => $"{Messages.LessThan} {value}.";
|
||||
public static string LessThanOrEqualTo(int value) => $"{Messages.LessThanOrEqualTo} {value}.";
|
||||
public static string GreaterThan(int value) => $"{Messages.GreaterThan} {value}.";
|
||||
public static string GreaterThanOrEqualTo(int value) => $"{Messages.GreaterThanOrEqualTo} {value}.";
|
||||
}
|
||||
|
||||
public static class Messages {
|
||||
public const string
|
||||
LessThan = "Less than",
|
||||
LessThanOrEqualTo = "Less than or equal to",
|
||||
GreaterThan = "Greater than",
|
||||
GreaterThanOrEqualTo = "Greater than or equal to";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user