Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
add extension methods for IEnumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames committed Nov 20, 2019
1 parent ad0b4a8 commit fb2398a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 22 deletions.
103 changes: 103 additions & 0 deletions src/Shriek/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Shriek
{
Expand Down Expand Up @@ -62,6 +64,29 @@ public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
}
}

public static async Task ForEachAsync<T>(this IEnumerable<T> items, Func<T, Task> func)
{
if (items == null)
{
return;
}

for (int i = 0; i < items.Count(); i++)
{
await func(items.ElementAt(i));
}
}

public static ParallelLoopResult ForEachParallel<T>(this IEnumerable<T> items, Action<T> action)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}

return Parallel.ForEach(items, action);
}

/// <summary>
/// Checks whether or not collection is null or empty. Assumes colleciton can be safely enumerated multiple times.
/// </summary>
Expand All @@ -78,5 +103,83 @@ public static bool IsNullOrEmpty(this IEnumerable @this)

return true;
}

/// <summary>
/// Check if an item is in a list.
/// </summary>
/// <param name="item">Item to check</param>
/// <param name="list">List of items</param>
/// <typeparam name="T">Type of the items</typeparam>
public static bool IsIn<T>(this T item, params T[] list)
{
return list.Contains(item);
}

/// <summary>
/// Check if an item is in a list.
/// </summary>
/// <param name="item">Item to check</param>
/// <param name="list">List of items</param>
/// <typeparam name="T">Type of the items</typeparam>
public static bool IsIn<T>(this T item, IEnumerable<T> list)
{
return list.Contains(item);
}

public static IEnumerable<T> Distinct<T>(this IEnumerable<T> source, Func<T, T, bool> comparer)
where T : class
{
return source.Distinct(new DynamicEqualityComparer<T>(comparer));
}

public static IEnumerable<T> OrderBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector, Func<TKey, TKey, int> comparer)
where T : class
where TKey : class
{
return source.OrderBy(keySelector, new DynamicComparer<TKey>(comparer));
}

public static IEnumerable<T> OrderByDescending<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector, Func<TKey, TKey, int> comparer)
where T : class
where TKey : class
{
return source.OrderByDescending(keySelector, new DynamicComparer<TKey>(comparer));
}

private sealed class DynamicEqualityComparer<T> : IEqualityComparer<T>
where T : class
{
private readonly Func<T, T, bool> func;

public DynamicEqualityComparer(Func<T, T, bool> func)
{
this.func = func;
}

public bool Equals(T x, T y)
{
return this.func(x, y);
}

public int GetHashCode(T obj)
{
return 0;
}
}

private sealed class DynamicComparer<T> : IComparer<T> where T : class
{
private readonly Func<T, T, int> func;

public DynamicComparer(Func<T, T, int> func)
{
this.func = func;
}

public int Compare(T x, T y)
{
return this.func(x, y);
}
}
}
}
22 changes: 0 additions & 22 deletions src/Shriek/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,5 @@ public static T ToEnum<T>(this string value, bool ignoreCase)

return (T)Enum.Parse(typeof(T), value, ignoreCase);
}

/// <summary>
/// Check if an item is in a list.
/// </summary>
/// <param name="item">Item to check</param>
/// <param name="list">List of items</param>
/// <typeparam name="T">Type of the items</typeparam>
public static bool IsIn<T>(this T item, params T[] list)
{
return list.Contains(item);
}

/// <summary>
/// Check if an item is in a list.
/// </summary>
/// <param name="item">Item to check</param>
/// <param name="list">List of items</param>
/// <typeparam name="T">Type of the items</typeparam>
public static bool IsIn<T>(this T item, IEnumerable<T> list)
{
return list.Contains(item);
}
}
}

0 comments on commit fb2398a

Please sign in to comment.