2 using System.Collections.Generic;
\r
5 public static class IEnumerableRocks
\r
8 public static string Implode<TSource, TResult> (this IEnumerable<TSource> self, string separator, Func<TSource, TResult> selector)
\r
10 return Implode (self, separator, (b, e) => { b.Append (selector (e).ToString ()); });
\r
13 public static string Implode<TSource> (this IEnumerable<TSource> self, string separator, Action<StringBuilder, TSource> appender)
\r
15 var coll = self as ICollection<TSource>;
\r
16 if (coll != null && coll.Count == 0)
\r
17 return string.Empty;
\r
19 bool needSep = false;
\r
20 var s = new StringBuilder ();
\r
22 foreach (var element in self) {
\r
23 if (needSep && separator != null)
\r
24 s.Append (separator);
\r
26 appender (s, element);
\r
30 return s.ToString ();
\r
36 public static void Main ()
\r
38 Console.WriteLine (new [] { "foo", "bar" }.Implode (", ", e => "'" + e + "'"));
\r