From 8e6f565702e00707e03f948c52af1398d561985c Mon Sep 17 00:00:00 2001 From: parsaManouchehrian Date: Sat, 20 Oct 2018 14:34:49 -0700 Subject: [PATCH 1/2] add FirstOrDefault --- ShittyLINQ/FirstOrDefault.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ShittyLINQ/FirstOrDefault.cs diff --git a/ShittyLINQ/FirstOrDefault.cs b/ShittyLINQ/FirstOrDefault.cs new file mode 100644 index 0000000..707d02e --- /dev/null +++ b/ShittyLINQ/FirstOrDefault.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static T FirstOrDefault(this IEnumerable self) + { + if (self == null) throw new ArgumentNullException(); + var iterator = self.GetEnumerator(); + if (!iterator.MoveNext()) return default(T); + return iterator.Current; + } + } +} From 52e57e3c2958845a78d87b949de49901ad96e327 Mon Sep 17 00:00:00 2001 From: parsaManouchehrian Date: Sat, 20 Oct 2018 14:47:19 -0700 Subject: [PATCH 2/2] create anouther count --- ShittyLINQ/Count.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ShittyLINQ/Count.cs b/ShittyLINQ/Count.cs index ca73d4a..24179f5 100644 --- a/ShittyLINQ/Count.cs +++ b/ShittyLINQ/Count.cs @@ -10,5 +10,17 @@ public static long Count(this IEnumerable self) { return self.Aggregate(0L, (memo, v) => ++memo); } + + public static long Count(this IEnumerable self , Func predicate) + { + if (self == null || predicate == null) throw new ArgumentNullException(); + Func, T, List> mapFunc = (memo, obj) => + { + if (predicate(obj)) memo.Add(obj); + return memo; + }; + var output = self.Foldl(new List(), mapFunc); + return output.Aggregate(0L, (memo, v) => ++memo); + } } }