Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emanuelefirmani/9e0367a7099ddfe046a9f057316dd1cf to your computer and use it in GitHub Desktop.
Save emanuelefirmani/9e0367a7099ddfe046a9f057316dd1cf to your computer and use it in GitHub Desktop.
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));
}
@emanuelefirmani
Copy link
Author

When converting line 20 to statement lambda, I get the following:

internal static Validator<T> ConditionExcludesChildren<T>(Func<T, bool> condition, IEnumerable<Validator<T>> children) =>
   value => condition(value) ? EmptyErrorList : Enumerable.Aggregate(children, value);

which does not even compile because children.Aggregate was replaced by Enumerable.Aggregate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment