Skip to content
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

Expunged all references to GoodLinq #3488

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mrsharm
Copy link
Member

@mrsharm mrsharm commented Nov 13, 2023

Renamed GoodLinq to EagerLinq and added it as a part of the System.Linq namespace as an extension method.

mrsharm and others added 2 commits November 13, 2023 14:57
Comment on lines +36 to +69
public static double EagerSum<TSource>(this IEnumerable<TSource> data, Func<TSource, double> map)
{
double sum = 0;
if (data == null || data.Count() == 0)
{
return sum;
}

foreach(var sourceItem in data)
{
sum += (double)map(sourceItem);
}

return sum;
}

public static double EagerAverage<TSource>(this IEnumerable<TSource> data, Func<TSource, double> map)
{
int count = data.Count();
if (data == null || count == 0)
{
return double.NaN;
}

double sum = 0;
foreach(var sourceItem in data)
{
sum += (double)map(sourceItem);
}

return sum / count;
}
}
}
Copy link
Member

@markples markples Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standard Linq methods are already eager for these two. These differ from Linq

  • in their handling of null (no exception)
  • Average() returning NaN instead of throwing. (though there are Linq overloads that takes enumerables of nullables, and if the enumerable has no nonnull values (empty enumerable is a degenerate case of this) returns null)
  • calling Count() rather than counting during the foreach - if the enumerable is a List this won't matter, but for others it could enumerate twice
  • Linq uses checked arithmetic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants