Skip to content

Instantly share code, notes, and snippets.

@janderit
Last active December 11, 2015 07:08
Show Gist options
  • Save janderit/4564158 to your computer and use it in GitHub Desktop.
Save janderit/4564158 to your computer and use it in GitHub Desktop.
My favourite collection extenders...
public static class CollectionExtender
{
public static IEnumerable<string> NonBlank(this IEnumerable<string> list)
{
return list.Where(_ => !String.IsNullOrWhiteSpace(_));
}
public static IEnumerable<T> NonNull<T>(this IEnumerable<T> list) where T:class
{
return list.Where(_ => null != (object) _);
}
public static IEnumerable<T> Zip2<T, T1, T2>(this IEnumerable<T> list)
{
return list.Where(t => t is T1 || t is T2);
}
public static string AggregateCsv<T>(this IEnumerable<T> source, string prefix="", string postfix="", string ifempty=null)
{
var result = prefix + source.Aggregate("", (l, i) => l + i + ", ", l => l.TrimEnd(' ', ',')) + postfix;
if (result == prefix + postfix && ifempty != null) return ifempty;
return result;
}
public static string Aggregate<T>(this IEnumerable<T> source, string globalprefix, string itemprefix, string itempostfix, string interitem, string globalpostfix, string emptyglobal=null)
{
var any = false;
var list = source.Aggregate(new StringBuilder(), (sb, item) =>
{
if (!any)
{
any = true;
}
else
{
sb.Append(interitem);
}
sb.Append(itemprefix);
sb.Append(item);
sb.Append(itempostfix);
return sb;
}, sb => sb.ToString());
return (any || emptyglobal == null) ? globalprefix + list + globalpostfix : emptyglobal;
}
public static void SplitCast<T, T1, T2>(this IEnumerable<T> source, Action<T1> action1, Action<T2> action2)
where T1 : T
where T2 : T
{
source.SplitCast<T, T1, T2, T, T, T, T>(action1, action2, null, null, null, null);
}
public static void SplitCast<T, T1, T2, T3>(this IEnumerable<T> source, Action<T1> action1, Action<T2> action2, Action<T3> action3)
where T1 : T
where T2 : T
where T3 : T
{
source.SplitCast<T, T1, T2, T3, T, T, T>(action1, action2, action3, null, null, null);
}
public static void SplitCast<T, T1, T2, T3, T4>(this IEnumerable<T> source, Action<T1> action1, Action<T2> action2, Action<T3> action3, Action<T4> action4)
where T1 : T
where T2 : T
where T3 : T
where T4 : T
{
source.SplitCast<T, T1, T2, T3, T4, T, T>(action1, action2, action3, action4, null, null);
}
public static void SplitCast<T, T1, T2, T3, T4, T5>(this IEnumerable<T> source, Action<T1> action1, Action<T2> action2, Action<T3> action3, Action<T4> action4, Action<T5> action5)
where T1 : T
where T2 : T
where T3 : T
where T4 : T
where T5 : T
{
source.SplitCast<T, T1, T2, T3, T4, T5, T>(action1, action2, action3, action4, action5, null);
}
public static void SplitCast<T, T1, T2, T3, T4, T5, T6>(this IEnumerable<T> source, Action<T1> action1, Action<T2> action2, Action<T3> action3, Action<T4> action4, Action<T5> action5, Action<T6> action6)
where T1 : T
where T2 : T
where T3 : T
where T4 : T
where T5 : T
where T6 : T
{
var configured = new List<Action<T>>();
if (action1 != null) configured.Add(t => { if (t is T1) action1((T1) t); });
if (action2 != null) configured.Add(t => { if (t is T2) action2((T2) t); });
if (action3 != null) configured.Add(t => { if (t is T3) action3((T3) t); });
if (action4 != null) configured.Add(t => { if (t is T4) action4((T4) t); });
if (action5 != null) configured.Add(t => { if (t is T5) action5((T5) t); });
if (action6 != null) configured.Add(t => { if (t is T6) action6((T6) t); });
foreach (var t in source) configured.ForEach(_ => _(t));
}
public static void AddEx<T>(this ICollection<T> collection, T item)
{
if (!collection.Contains(item)) collection.Add(item);
}
public static void RemoveEx<T>(this ICollection<T> collection, T item)
{
if (collection.Contains(item)) collection.Remove(item);
}
public static bool HeadTail<T>(this IEnumerable<T> source, out T head, out IEnumerable<T> tail)
{
var thelist = new List<T>();
tail = thelist;
var enumerator = source.GetEnumerator();
if (enumerator.MoveNext())
{
head = enumerator.Current;
}
else
{
head = default(T);
return false;
}
while (enumerator.MoveNext()) thelist.Add(enumerator.Current);
return true;
}
public static bool InitLast<T>(this IEnumerable<T> source, out IEnumerable<T> init, out T tail)
{
var thelist = new List<T>();
init = thelist;
var enumerator = source.GetEnumerator();
T last = default(T);
if (!enumerator.MoveNext())
{
tail = last;
return false;
}
last = enumerator.Current;
while (enumerator.MoveNext())
{
thelist.Add(last);
last = enumerator.Current;
}
tail = last;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment