Skip to content

Commit

Permalink
feat: added intersect many method
Browse files Browse the repository at this point in the history
  • Loading branch information
aochmann committed Feb 19, 2021
1 parent 1c5370b commit 6dd8b20
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Source/Cogworks.Essentials/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,31 @@ public static bool HasAny(this IEnumerable items)
public static bool HasAny<T>(this IEnumerable<T> items)
=> items != null && items.GetEnumerator().MoveNext();

public static TOutput FirstOrDefaultOfType<TInput, TOutput>(this IEnumerable publishedContents)
=> publishedContents.OfType<TOutput>().FirstOrDefault();
public static TOutput FirstOrDefaultOfType<TOutput>(this IEnumerable items)
=> items.OfType<TOutput>().FirstOrDefault();

public static TOutput FirstOrDefaultOfType<TInput, TOutput>(this IEnumerable<TInput> items)
=> items.OfType<TOutput>().FirstOrDefault();

public static string JoinIfNotNull<TInput, TResult>(this IEnumerable<TInput> items, Func<TInput, TResult> func,
string separator = Separators.Space)
=> items.HasAny()
? string.Join(separator, items.Select(func))
: string.Empty;

public static IEnumerable<T> IntersectManyWithHash<T>(this IEnumerable<IEnumerable<T>> values)
=> !values.Any()
? Enumerable.Empty<T>()
: values
.Skip(1)
.Aggregate(
new HashSet<T>(values.First()),
(h, e) =>
{
h.IntersectWith(e);
return h;
});

public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> items)
=> items.HasAny()
? items
Expand Down

0 comments on commit 6dd8b20

Please sign in to comment.