Comma quibbling a la Eric and Jafar [A slightly wacky approach to the problem in C#]
Jafar was teasing me about his F# solution to Eric Lippert's comma quibbling problem, and I decided to retaliate. :) One of the things I didn't like about the five-or-so solutions I'd seen [including Jafar's :P ] was that they were doing a bunch of work to detect special cases on every pass through the loop. That seemed silly to me because there are really only two special cases and they only come into play at the very end of the operation.
So I threw together the following method which has a fairly efficient inner loop which saves the special cases for the end. I also made it generic so it'll take an input stream of any type - then defer to StringBuilder
(or the object itself!) for proper formatting. Oh, and the code is both compact and commented. :)
The downside - and it's a big one - is that the input stream is enumerated three times instead of just one. :(
Oh well, when you're out for a quick bit of revenge you can't accomplish everything... :)
/// <summary> /// Solve comma quibbling posed by Eric Lippert at: /// http://blogs.msdn.com/ericlippert/archive/2009/04/15/comma-quibbling.aspx. /// </summary> /// <typeparam name="T">Type of input.</typeparam> /// <param name="input">Stream of input elements (ex: string).</param> /// <returns>Quibbled string.</returns> /// <remarks> /// Good points: /// * Generic type support (StringBuilder formats for output) /// * Special-case logic is not run every time through the loop /// Bad points: /// * Input stream is traversed three times :( /// </remarks> private static string CommaQuibbling<T>(IEnumerable<T> input) { // Capture stream var a = input.GetEnumerator(); var b = input.Skip(1).GetEnumerator(); var c = input.Skip(2).GetEnumerator(); // Prefix the result var sb = new StringBuilder("{"); // Process the "normal" leading elements while (c.MoveNext() && b.MoveNext() && a.MoveNext()) { sb.Append(a.Current).Append(", "); } // Process the non-Oxford comma scenario if (b.MoveNext() && a.MoveNext()) { sb.Append(a.Current).Append(" and "); } // Process the remaining element if (a.MoveNext()) { sb.Append(a.Current); } // Postfix the result and return it return sb.Append("}").ToString(); }