Challenge: Robust enumeration over external code
Here is an interesting little problem:
public class Program
{
private static void Main()
{
foreach (int i in RobustEnumerating(Enumerable.Range(0, 10), FaultyFunc))
{
Console.WriteLine(i);
}
}
public static IEnumerable<T> RobustEnumerating<T>(
IEnumerable<T> input,Func<IEnumerable<T>, IEnumerable<T>> func)
{
// how to do this?
return func(input);
}
public static IEnumerable<int> FaultyFunc(IEnumerable<int> source)
{
foreach (int i in source)
{
yield return i/(i%2);
}
}
}
This code should not throw, but print:
1
3
5
7
9
Can you make this happen? You can only change the RobustEnumerating method, nothing else in the code