I just stumbled upon this piece of code, I was wondering why the graph is executed during the loop.
public static int FindIndex<T>(this IEnumerable<T> source, Predicate<T> predicate)
{
for (int i = 0; i < source.Count(); i++)
{
if (predicate(source.ElementAt(i))) return i;
}
return -1;
}
If the counter can change, we should not just do it like this: for (int i = source.Count () - 1; i> = 0; i -)
Otherwise, I think we should calculate the score before the start of the cycle, and not every time.
What would be the right way to do this?
source
share