Skip to content

Instantly share code, notes, and snippets.

@k-maru
Last active September 27, 2017 05:47
Show Gist options
  • Save k-maru/6c98c1729a6ede6d2c76 to your computer and use it in GitHub Desktop.
Save k-maru/6c98c1729a6ede6d2c76 to your computer and use it in GitHub Desktop.
Linq Extensions
public static IEnumerable<T> Action<T>(this IEnumerable<T> source, Action<T> onNext)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (onNext == null) throw new ArgumentNullException(nameof(onNext));
return Action_(source, onNext, e => { }, () => { });
}
public static IEnumerable<T> Action<T>(this IEnumerable<T> source, Action<T> onNext, Action<Exception> onError)
{
if (source == null) throw new ArgumentNullException(namoef(source));
if (onNext == null) throw new ArgumentNullException(namoef(onNext));
if (onError == null) throw new ArgumentNullException(namoef(onError));
return Action_(source, onNext, onError, () => { });
}
public static IEnumerable<T> Action<T>(this IEnumerable<T> source, Action<T> onNext, Action onFinally)
{
if (source == null) throw new ArgumentNullException(namoef(source));
if (onNext == null) throw new ArgumentNullException(namoef(onNext));
if (onFinally == null) throw new ArgumentNullException(namoef(onFinally));
return Action_(source, onNext, e => { }, onFinally);
}
public static IEnumerable<T> Action<T>(this IEnumerable<T> source, Action<T> onNext, Action<Exception> onError, Action onFinally)
{
if (source == null) throw new ArgumentNullException(namoef(source));
if (onNext == null) throw new ArgumentNullException(namoef(onNext));
if (onError == null) throw new ArgumentNullException(namoef(onError));
if (onFinally == null) throw new ArgumentNullException(namoef(onFinally));
return Action_(source, onNext, onError, onFinally);
}
private static IEnumerable<T> Action_<T>(IEnumerable<T> source, Action<T> onNext, Action<Exception> onError, Action onFinally)
{
using(var e = source.GetEnumerator())
{
var current = default(T);
while (true)
{
try
{
if (!e.MoveNext())
{
break;
}
current = e.Current;
onNext(current);
}
catch (Exception ex)
{
onError(ex);
throw;
}
yield return current;
}
onFinally();
}
}
public static string Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, int, TSource> func)
{
if (source == null) throw new ArgumentNullException(naeof(source));
if (func == null) throw new ArgumentNullException(nameof(func));
var index = 1;
using (var enumerator = source.GetEnumerator())
{
if (!enumerator.MoveNext())
{
throw new InvalidOperationException("Element not found.");
}
var current = enumerator.Current;
while (enumerator.MoveNext())
{
current = func(current, enumerator.Current, index++);
}
return current;
}
}
public static TAccumulate Aggregate<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, int, TAccumulate> func)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (func == null) throw new ArgumentNullException(nameof(func));
var index = 0;
var item = seed;
foreach (var item1 in source)
{
item = func(item, item1, index++);
}
return item;
}
public static TResult Aggregate<TSource, TAccumulate, TResult>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, int, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (format == null) throw new ArgumentNullException(nameof(format));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
var index = 0;
var item = seed;
foreach (var item1 in source)
{
item = func(item, item1, index++);
}
return resultSelector(item);
}
public static string ConcatWith<T>(this IEnumerable<T> source, string separator)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return string.Join(separator, source);
}
public static string ConcatWith<T>(this IEnumerable<T> source, string separator, string format, IFormatProvider formatProvider = null)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (format == null) throw new ArgumentNullException(nameof(format));
if (formatProvider != null)
{
return source.Select(s => string.Format(formatProvider, format, s)).ConcatWith(separator);
}
return source.Select(s => string.Format(format, s)).ConcatWith(separator);
}
public static int FindIndex<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
int retVal = 0;
foreach (var item in source)
{
if (predicate(item))
{
return retVal;
}
retVal++;
}
return -1;
}
public static T FirstOrDefault<T>(this IEnumerable<T> source, T defaultValue)
{
if (source.Any())
{
return source.First();
}
return defaultValue;
}
public static T FirstOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate, T defaultValue)
{
if (source.Any())
{
var result = source.Where(predicate).FirstOrDefault(defaultValue);
}
return defaultValue;
}
/// <summary>
/// シーケンスが <see cref="null"/> もしくは要素が含まれていないかどうかを判断します。
/// </summary>
/// <typeparam name="T">コレクションのタイプ</typeparam>
/// <param name="source">シーケンス</param>
/// <returns>シーケンスが <see cref="null"/> もしくは要素が含まれていない場合は true , そうでない場合は false</returns>
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source) => source != null && source.Any();
/// <summary>
/// 指定されたシーケンスが null だった場合に、空のシーケンスを返します。
/// </summary>
/// <typeparam name="T">コレクションのタイプ</typeparam>
/// <param name="source">シーケンス</param>
/// <returns>指定されたシーケンスが null だった場合は空のシーケンス、そうでない場合は指定されたシーケンスのインスタンス</returns>
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source) => source == null ? Enumerable.Empty<T>() : source;
public static IEnumerable<T> WithoutLast<T>(this IEnumerable<T> source)
{
if(source == null) throw new ArgumentNullException(nameof(source));
var lastItem = default(T);
var first = true;
foreach (var item in source)
{
if (first)
{
first = false;
}
else
{
yield return lastItem;
}
lastItem = item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment