Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ShittyLINQ/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ public static long Count<T>(this IEnumerable<T> self)
{
return self.Aggregate(0L, (memo, v) => ++memo);
}

public static long Count<T>(this IEnumerable<T> self , Func<T, bool> predicate)
{
if (self == null || predicate == null) throw new ArgumentNullException();
Func<List<T>, T, List<T>> mapFunc = (memo, obj) =>
{
if (predicate(obj)) memo.Add(obj);
return memo;
};
var output = self.Foldl(new List<T>(), mapFunc);
return output.Aggregate(0L, (memo, v) => ++memo);
}
}
}
17 changes: 17 additions & 0 deletions ShittyLINQ/FirstOrDefault.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ShittyLINQ
{
public static partial class Extensions
{
public static T FirstOrDefault<T>(this IEnumerable<T> self)
{
if (self == null) throw new ArgumentNullException();
var iterator = self.GetEnumerator();
if (!iterator.MoveNext()) return default(T);
return iterator.Current;
}
}
}