﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>James commented on Challenge: Robust enumeration over external code</title><description>Simplest possible solution ...
  
  
		public static IEnumerable
&lt;int RobustEnumerating
&lt;t(IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
		{
  
			return new[] { 1, 3, 5, 7, 9 };
  
		}
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment98</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment98</guid><pubDate>Thu, 11 Mar 2010 10:07:07 GMT</pubDate></item><item><title>sergio commented on Challenge: Robust enumeration over external code</title><description>Maybe its not the best, but it works
  
public class Program
  
    {
  
        private static void Main()
  
        {
  
            foreach (int i in RobustEnumerating(Enumerable.Range(0, 10), FaultyFunc))
  
            {
  
                Console.WriteLine(i);
  
            }
  
            Console.ReadLine();
  
        }
  
  
        public static IEnumerable
&lt;t RobustEnumerating
&lt;t(
  
            IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
        {
  
            // how to do this?
  
            var enumerator = func(input).GetEnumerator();
  
            while (SafeMoveNextEnumerator(input, func, ref enumerator))
  
            {
  
                yield return enumerator.Current;
  
            }
  
  
  
  
        }
  
        static int skip = 0;
  
  
        public static bool SafeMoveNextEnumerator
&lt;t(IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func, ref IEnumerator
&lt;t safe)
  
        {
  
            while (true)
  
            {
  
                skip++;
  
                try
  
                {
  
                    return safe.MoveNext();
  
                }
  
                catch
  
                {
  
                    var e = Enumerable.Skip(input, skip);
  
                    safe = func(e).GetEnumerator();
  
                }
  
            }
  
        }
  
  
        public static IEnumerable
&lt;int FaultyFunc(IEnumerable
&lt;int source)
  
        {
  
            foreach (int i in source)
  
            {
  
                yield return i / (i % 2);
  
            }
  
        }
  
    }
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment97</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment97</guid><pubDate>Tue, 09 Mar 2010 08:01:30 GMT</pubDate></item><item><title>firefly commented on Challenge: Robust enumeration over external code</title><description>@ tobi, it's not hard but I wouldn't say it's intuitive either. Especially for an interview question unless you've wrote library code where you use MoveNext a lot and have an intimate understanding of how it really work.
  
  
Now if you've been exposed to this before somewhere else then it's extremely easy :). Thanks Oren and everyone else that comments. I learned a lot from the exposure... especially from Demid code, very elegant indeed.
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment96</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment96</guid><pubDate>Fri, 05 Mar 2010 21:44:35 GMT</pubDate></item><item><title>Chris Martin commented on Challenge: Robust enumeration over external code</title><description>public static IEnumerable
&lt;int RobustEnumerating(IEnumerable
&lt;int input, Func
&lt;ienumerable&lt;int, IEnumerable
&lt;int&gt; func)
  
{
  
	return func(input.ToList().ConvertAll(t =&gt; int.Parse(t.ToString())).Where(i =&gt; i%2 &gt; 0));
  
}
  
  
lol
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment95</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment95</guid><pubDate>Fri, 05 Mar 2010 18:30:49 GMT</pubDate></item><item><title>Thomas Eyde commented on Challenge: Robust enumeration over external code</title><description>It looks like the solution is a variation over iterating the input, because that's the only thing we can control, evaluate each item and only return the results of those evaluations that don't fail.
  
  
Here's my attempt:
  
  
  
	            foreach (var item in input)
  
	            {
  
	                T[] evaluatedItems;
  
	                try
  
	                {
  
	                    var itemAsEnumerable = new[] { item };
  
	                    evaluatedItems = func(itemAsEnumerable).ToArray();
  
	                }
  
	                catch (Exception)
  
	                {
  
	                    continue;
  
	                }
  
  
	                foreach (var evaluated in evaluatedItems)
  
	                {
  
	                    yield return evaluated;
  
	                }
  
	            }
  
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment94</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment94</guid><pubDate>Fri, 05 Mar 2010 13:59:18 GMT</pubDate></item><item><title>Allan PArker commented on Challenge: Robust enumeration over external code</title><description>Hi,
  
  
Here is my attempt at this:
  
  
  
        public static IEnumerable
&lt;t RobustEnumerating
&lt;t(IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
        {
  
            foreach (T item in input)
  
            {
  
                IEnumerable
&lt;t result;
  
                var list = new List
&lt;t { item };
  
  
                try
  
                {
  
                    result = func(list);
  
                    var test = result.First();
  
                }
  
                catch
  
                {
  
                    continue;
  
                }
  
  
                yield return result.First();
  
            }
  
        }
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment93</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment93</guid><pubDate>Fri, 05 Mar 2010 11:04:06 GMT</pubDate></item><item><title>Sergey Kachkovsky commented on Challenge: Robust enumeration over external code</title><description>Ok, and what is the BEST solution?
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment92</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment92</guid><pubDate>Fri, 05 Mar 2010 10:58:57 GMT</pubDate></item><item><title>Ken Tong commented on Challenge: Robust enumeration over external code</title><description>How about this?
  
  
  
	public static IEnumerable&lt;T&gt; RobustEnumerating&lt;T&gt;(
  
	    IEnumerable&lt;T&gt; input, Func&lt;IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;&gt; func)
  
	{
  
	    IEnumerable&lt;T&gt; results = new T[0];
  
  
	    foreach (T item in input)
  
	    {
  
	        try
  
	        {
  
	            foreach (T value in func(new[] { item }))
  
	                results = results.Concat(new[] { value });
  
	        }
  
	        catch { }
  
	    }
  
  
	    foreach (T result in results)
  
	        yield return result;
  
	}
  
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment91</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment91</guid><pubDate>Fri, 05 Mar 2010 02:38:10 GMT</pubDate></item><item><title>Ajai Shankar commented on Challenge: Robust enumeration over external code</title><description>@Demid &amp; Dmitriy: vi &amp; csc works too :-)
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment90</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment90</guid><pubDate>Fri, 05 Mar 2010 02:01:44 GMT</pubDate></item><item><title>Dmitriy Nagirnyak commented on Challenge: Robust enumeration over external code</title><description>@Demind, I use LINQPad for that purpose. Copy-paste code and it just runs.
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment89</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment89</guid><pubDate>Fri, 05 Mar 2010 00:57:52 GMT</pubDate></item><item><title>Demid commented on Challenge: Robust enumeration over external code</title><description>I can't believe so many people are so lazy to compile their code at least.
  
  
It's not a crime do not know that try/catch block isn't compatible with yield statement but you can at least try to compile it. 
  
  
Download SnippetCompiler. It is outdated project but it really really good to test your ideas whenever you're not sure how compiler or .net behaves in a particular situation because. It loaded instantly and I ran Oren's example in two seconds after I saw it.
  
  
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment88</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment88</guid><pubDate>Fri, 05 Mar 2010 00:13:43 GMT</pubDate></item><item><title>James C-S commented on Challenge: Robust enumeration over external code</title><description>Whoops! Now I feel bad about me, err my, grammar... ;-)
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment87</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment87</guid><pubDate>Fri, 05 Mar 2010 00:11:37 GMT</pubDate></item><item><title>James C-S commented on Challenge: Robust enumeration over external code</title><description>OK. I felt bad about me previous answer that broke encapsulation. Here's an answer that doesn't.
  
  
        public static IEnumerable
&lt;t RobustEnumerating
&lt;t(
  
            IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
        {
  
            Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; safeFunc = source =&gt;
  
            {
  
                var @return = (IEnumerable
&lt;t)null;
  
                try { @return = func(source).ToArray(); }
  
                catch (System.DivideByZeroException) { }
  
                return @return;
  
            };
  
            return (from i2 in
  
                        (from i in input
  
                         select new T[] { i })
  
                    let o2 = safeFunc(i2)
  
                    where o2 != null
  
                    select o2).SelectMany(o =&gt; o);
  
        }
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment86</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment86</guid><pubDate>Fri, 05 Mar 2010 00:10:29 GMT</pubDate></item><item><title>James C-S commented on Challenge: Robust enumeration over external code</title><description>This is probably not in the spirit of the challenge, but ...
  
  
    public static IEnumerable
&lt;t RobustEnumerating
&lt;t(
  
        IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
    {
  
        return func(input.Cast
&lt;int().Where(x =&gt; x % 2 != 0).Cast
&lt;t());
  
    }
  
  
...works just fine. Of course it does break encapsulation. :-)
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment85</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment85</guid><pubDate>Thu, 04 Mar 2010 23:46:04 GMT</pubDate></item><item><title>tobi commented on Challenge: Robust enumeration over external code</title><description>Wow I will remember this challenge and use it as an interview question. It seems like everyone gets it wrong the first time so you can discuss the problem with the candidate to see if they are smart or not.
  
  
Also I do not understand why everyone seems to restart the sequence when an error has occurred. Just do nothing or else the sequence will restart over and over.
  
  
I do not understand the purpose of StatefullEnumerable. It seems to do nothing.
  
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment84</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment84</guid><pubDate>Thu, 04 Mar 2010 18:52:57 GMT</pubDate></item><item><title>Kvasov Roman commented on Challenge: Robust enumeration over external code</title><description>I think it`s more robust implementation with additional class, but it call function only once so if it contains some initialization logic it doesn`t break. 
  
  
	using System;
  
	using System.Collections;
  
	using System.Collections.Generic;
  
	using System.Linq;
  
  
	public class Program
  
	{
  
	    public class StatefullEnumerable
&lt;t	 : IEnumerable
&lt;t	 {
  
  
	        private IEnumerator
&lt;t	 enumerator;
  
  
	        public StatefullEnumerable(IEnumerable
&lt;t	 enumerable) {
  
	            enumerator = enumerable.GetEnumerator();
  
	        }
  
  
	        public IEnumerator
&lt;t	 GetEnumerator() {
  
	            return enumerator;
  
	        }
  
  
	        IEnumerator IEnumerable.GetEnumerator() {
  
	            return GetEnumerator();
  
	        }
  
	    }
  
  
	    private static void Main()
  
	    {
  
	        foreach (int i in RobustEnumerating(Enumerable.Range(0, 10), FaultyFunc))
  
	        {
  
	            Console.WriteLine(i);
  
	        }
  
	    }
  
  
	    public static IEnumerable
&lt;t	 RobustEnumerating
&lt;t	(
  
	        IEnumerable
&lt;t	 input,Func
&lt;ienumerable&lt;t	, IEnumerable
&lt;t	&gt; func)
  
	    {
  
	        var enumerable = func(new StatefullEnumerable
&lt;t	(input));
  
	        var enumerator = enumerable.GetEnumerator();
  
	        while (true) {
  
	            var fail = false;
  
	            var value = default(T);
  
	            try {
  
	                if (!enumerator.MoveNext())
  
	                    yield break;
  
	                value = enumerator.Current;
  
	            }
  
	            catch (Exception) {
  
	                fail = true;
  
	                enumerator = enumerable.GetEnumerator();
  
	            }
  
	            if (!fail)
  
	                yield return value;
  
	        }
  
	        // how to do this?
  
	    }
  
  
	    public static IEnumerable
&lt;int	 FaultyFunc(IEnumerable
&lt;int	 source)
  
	    {
  
	        foreach (int i in source)
  
	        {
  
	            yield return i/(i%2);
  
	        }
  
	    }
  
	}
  
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment83</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment83</guid><pubDate>Thu, 04 Mar 2010 17:44:57 GMT</pubDate></item><item><title>Ajai Shankar commented on Challenge: Robust enumeration over external code</title><description>@Ayende "Ajai, You are iterating over the input enum more than once"
  
  
Here is what I think works (I had to add a method, don't know if it is allowed)
  
  
Very nice problem, do you expect the input enumerable to throw too?
  
  
Now I need to go get some real work done :-)
  
  
Looking forward to what you have, can't take the suspense any longer!
  
  
	static IEnumerable
&lt;t EnumOnce
&lt;t(IEnumerator
&lt;t e)
  
	{
  
		while(e.MoveNext())
  
			yield return e.Current;
  
	}
  
  
    public static IEnumerable
&lt;t RobustEnumerating
&lt;t(
  
        IEnumerable
&lt;t input,Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
    {
  
		input = EnumOnce(input.GetEnumerator());
  
  
		var result = func(input).GetEnumerator();
  
		while(true) {
  
			try {
  
				if(!result.MoveNext())
  
					break;
  
			} catch(Exception) {
  
				result = func(input).GetEnumerator();
  
				continue;
  
			}
  
			yield return result.Current;
  
	 	}
  
    }
  
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment82</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment82</guid><pubDate>Thu, 04 Mar 2010 17:09:56 GMT</pubDate></item><item><title>Michal commented on Challenge: Robust enumeration over external code</title><description>return input.Select(i =&gt;
  
                        {
  
                            try
  
                            {
  
                                return func(new[] { i }).First();
  
                            }
  
                            catch
  
                            {
  
                                return default(T);
  
                            }
  
                        }).
  
                    Where(i =&gt; !i.Equals(default(T)));
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment81</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment81</guid><pubDate>Thu, 04 Mar 2010 15:38:17 GMT</pubDate></item><item><title>tobi commented on Challenge: Robust enumeration over external code</title><description>the func transforms a non-throwing sequence into a throwing sequence. you can solve it calling it only once.
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment80</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment80</guid><pubDate>Thu, 04 Mar 2010 15:33:52 GMT</pubDate></item><item><title>Mike Thomas commented on Challenge: Robust enumeration over external code</title><description>@ayende or if MoveNext just sets some other flag before it throws or if MoveNext disposes of itself before it throws or if MoveNext does something like this (assuming for the moment that I still reset the state correctly):
  
  
yield return memberCounterThatINeedToBeUniquePerIncomingItem + transformThatWontThrow();
  
  
yield return memberCounterThatINeedToBeUniquePerIncomingItem + transformThatWillThrow();
  
  
memberCounterThatINeedToBeUniquePerIncomingItem++;
  
  
Basically, like Nick says, if we have the requirement that the func can be called only once (which makes sense since it could be keeping track of its position or could make a million expensive remote calls before returning the first item) you can't solve this in a generic way.
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment79</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment79</guid><pubDate>Thu, 04 Mar 2010 15:10:56 GMT</pubDate></item><item><title>tobi commented on Challenge: Robust enumeration over external code</title><description>I cannot believe that 95% of all answers are just wrong. It is not that hard... If your call to movenext is not in a try-catch, your code is wrong. if you use foreach, your code is wrong because an exception will skip the rest of the enumeration. Do not call func multiple times, it is not necessary. It will enumerate the sequence multiple times.
  
Nice challenge however.
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment78</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment78</guid><pubDate>Thu, 04 Mar 2010 13:16:51 GMT</pubDate></item><item><title>Sergey Shishkin commented on Challenge: Robust enumeration over external code</title><description>            return input.SelectMany(x =&gt;
  
            {
  
                try
  
                {
  
                    return new[]{func(new[] { x }).FirstOrDefault()};
  
                }
  
                catch
  
                {
  
                    return new T[0];
  
                }
  
            });
  
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment77</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment77</guid><pubDate>Thu, 04 Mar 2010 13:00:19 GMT</pubDate></item><item><title>Frank commented on Challenge: Robust enumeration over external code</title><description>Without having looked at previous posted entries, this should do the trick, while still supporting infinite enumerations:
  
  
public static IEnumerable
&lt;t RobustEnumerating
&lt;t  
(
  
    IEnumerable
&lt;t input,
  
    Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func
  
)
  
{
  
    var singleInputElementArray = new T[1];
  
    foreach (var inputElement in input)
  
    {
  
        singleInputElementArray[0] = inputElement;
  
  
        IEnumerator
&lt;t enumerator = func(singleInputElementArray).GetEnumerator();
  
        bool success;
  
        do
  
        {
  
            success = false;
  
  
            try
  
            {
  
                if (enumerator.MoveNext())
  
                    success = true;
  
            }
  
            catch
  
            {
  
            }
  
  
            if (success)
  
                yield return enumerator.Current;
  
        }
  
        while (success);
  
    }
  
}
  
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment76</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment76</guid><pubDate>Thu, 04 Mar 2010 12:17:41 GMT</pubDate></item><item><title>Mike Doherty commented on Challenge: Robust enumeration over external code</title><description>This time I've escaped the angle brackets to see if that slips past the comment sanitizer.
  
  
Looking over Dimid's solution though I realize that I am repeatedly enumerating over the input which could have a performance impact if getting these values was expensive. I love the way that he wraps the input to preserve it's state.
  
  
  
	public static IEnumerable&lt;T&gt; RobustEnumerating&lt;T&gt;(
  
	   IEnumerable&lt;T&gt; input, Func&lt;IEnumerable&lt;T&gt;, IEnumerable&lt;T&gt;&gt; func)
  
	{
  
	   var rowsToSkipOnError = 1;
  
	   var enumerationToWrap = func(input).GetEnumerator();
  
	   while (true)
  
	   {
  
	       T nextValue;
  
	       try
  
	       {
  
	           if (!enumerationToWrap.MoveNext())
  
	               yield break;
  
  
	           nextValue = enumerationToWrap.Current;
  
	       }
  
	       catch (Exception)
  
	       {
  
	           // This input failed so skip to the next one
  
	           input = input.Skip(rowsToSkipOnError);
  
	           enumerationToWrap = func(input).GetEnumerator();
  
	           rowsToSkipOnError = 1;
  
	           continue;
  
	       }
  
  
	       rowsToSkipOnError++;
  
	       yield return nextValue;
  
	   }
  
	}
  
  
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment75</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment75</guid><pubDate>Thu, 04 Mar 2010 11:49:16 GMT</pubDate></item><item><title>Arielr commented on Challenge: Robust enumeration over external code</title><description>return input.SelectMany(x =&gt;
  
                            {
  
                                try
  
                                {
  
                                    return func(x.AsEnumerableX()).ToArray();
  
                                }
  
                                catch
  
                                {
  
                                    return Enumerable.Empty
&lt;t();
  
                                }
  
                            });
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment74</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment74</guid><pubDate>Thu, 04 Mar 2010 11:30:03 GMT</pubDate></item><item><title>Mike Doherty commented on Challenge: Robust enumeration over external code</title><description>Ok. Here is my solution:
  
  
  
	public static IEnumerable
&lt;t	 RobustEnumerating
&lt;t	(
  
	    IEnumerable
&lt;t	 input, Func
&lt;ienumerable&lt;t	, IEnumerable
&lt;t	&gt; func)
  
	{
  
	    var rowsToSkipOnError = 1;
  
	    var enumerationToWrap = func(input).GetEnumerator();
  
	    while (true)
  
	    {
  
	        T nextValue;
  
	        try
  
	        {
  
	            if (!enumerationToWrap.MoveNext())
  
	                yield break;
  
  
	            nextValue = enumerationToWrap.Current;
  
	        }
  
	        catch (Exception)
  
	        {
  
	            // This input failed so skip to the next one
  
	            input = input.Skip(rowsToSkipOnError);
  
	            enumerationToWrap = func(input).GetEnumerator();
  
	            rowsToSkipOnError = 1;
  
	            continue;
  
	        }
  
  
	        rowsToSkipOnError++;
  
	        yield return nextValue;
  
	    }
  
	}
  
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment73</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment73</guid><pubDate>Thu, 04 Mar 2010 11:26:48 GMT</pubDate></item><item><title>Sergey Kachkovsky commented on Challenge: Robust enumeration over external code</title><description>public static IEnumerable
&lt;t RobustEnumerating
&lt;t(
  
		    IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
		{
  
			IEnumerator
&lt;t enumerator = func(input).GetEnumerator();
  
			do
  
			{
  
				bool valid = true;
  
				try
  
				{
  
					if (!enumerator.MoveNext()) yield break;
  
				}
  
				catch
  
				{
  
					valid = false;
  
				}
  
				if (valid)
  
					yield return enumerator.Current;
  
			} while (true);
  
		}
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment72</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment72</guid><pubDate>Thu, 04 Mar 2010 11:14:37 GMT</pubDate></item><item><title>Koen commented on Challenge: Robust enumeration over external code</title><description>My generic type parameters weren't automatically HTML encoded so here's a second attempt encoding them myself:
  
return func(input.Cast&lt;int&gt;().Where(i =&gt; i % 2 == 1).Cast&lt;T&gt;());
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment71</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment71</guid><pubDate>Thu, 04 Mar 2010 10:28:40 GMT</pubDate></item><item><title>Alex Yakunin commented on Challenge: Robust enumeration over external code</title><description>@ Nick Aceves
  
  
You're right - I really made a mistake. Btw, such OnErrorResumeNext is fitting very well on their sequence concept (throw terminates a sequence). 
  
  
So now it looks like a tricky case to implement this fully on default LINQ combinators and Rx.
</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment70</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment70</guid><pubDate>Thu, 04 Mar 2010 10:14:26 GMT</pubDate></item><item><title>Benny Thomas commented on Challenge: Robust enumeration over external code</title><description>Ugly code that needs some tuning.
  
  
public static IEnumerable
&lt;t RobustEnumerating
&lt;t(
  
        IEnumerable
&lt;t input, Func
&lt;ienumerable&lt;t, IEnumerable
&lt;t&gt; func)
  
    {
  
  
        foreach(T a in input)
  
        {
  
            List
&lt;t results = new List
&lt;t();
  
            try
  
            {
  
                results.AddRange(func(new List
&lt;t {a}));
  
            }
  
            catch
  
            {
  
                // Do some faulthandling here
  
            }
  
            foreach (var result in results)
  
            {
  
                yield return result;
  
            }
  
        }        
  
    }
&gt;</description><link>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment69</link><guid>http://ayende.com/4420/challenge-robust-enumeration-over-external-code#comment69</guid><pubDate>Thu, 04 Mar 2010 10:03:29 GMT</pubDate></item></channel></rss>