Created
July 15, 2023 11:20
-
-
Save emanuelefirmani/9e0367a7099ddfe046a9f057316dd1cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyNamespace; | |
internal static class Rules | |
{ | |
internal static IEnumerable<string> EmptyErrorList { get; } = Array.Empty<string>(); | |
internal delegate IEnumerable<string> Validator<T>(T value); | |
internal static Validator<T> ParentOrChildren<T>(Validator<T> parent, IEnumerable<Validator<T>> children) => | |
value => | |
{ | |
var p = parent(value).ToArray(); | |
return p.Any() | |
? p | |
: children.Aggregate(value); | |
}; | |
internal static Validator<T> ConditionExcludesChildren<T>(Func<T, bool> condition, IEnumerable<Validator<T>> children) => | |
value => | |
{ | |
return condition(value) ? EmptyErrorList : children.Aggregate(value); | |
}; | |
private static IEnumerable<string> Aggregate<T>(this IEnumerable<Validator<T>> children, T value) => | |
children.Select(c => c(value)).Aggregate(EmptyErrorList, (e1, e2) => e1.Concat(e2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When converting line 20 to statement lambda, I get the following:
which does not even compile because
children.Aggregate
was replaced byEnumerable.Aggregate