Skip to content

Commit b717546

Browse files
committed
Some new IEnumerable extensions method
1 parent 1a5a3d4 commit b717546

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
12-
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
12+
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

SharpHelpers/SharpHelpers/EnumerableHelper.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,125 @@ public static IEnumerable<List<T>> Split<T>(this IEnumerable<T> enumerable, int
148148
}
149149
return splitList;
150150
}
151+
152+
/// <summary>
153+
/// Check if the list is null or empty.
154+
/// </summary>
155+
/// <typeparam name="T"></typeparam>
156+
/// <param name="source"></param>
157+
/// <returns></returns>
158+
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
159+
{
160+
return source == null || !source.Any();
161+
}
162+
163+
/// <summary>
164+
/// Apply an action to each element of the list.
165+
/// </summary>
166+
/// <typeparam name="T"></typeparam>
167+
/// <param name="source"></param>
168+
/// <param name="action"></param>
169+
/// <exception cref="ArgumentNullException"></exception>
170+
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
171+
{
172+
if (source == null) throw new ArgumentNullException(nameof(source));
173+
if (action == null) throw new ArgumentNullException(nameof(action));
174+
175+
foreach (var item in source)
176+
{
177+
action(item);
178+
}
179+
}
180+
181+
/// <summary>
182+
/// Chunk the list into sublists of the specified size.
183+
/// </summary>
184+
/// <typeparam name="T"></typeparam>
185+
/// <param name="source"></param>
186+
/// <param name="size"></param>
187+
/// <returns></returns>
188+
/// <exception cref="ArgumentNullException"></exception>
189+
/// <exception cref="ArgumentOutOfRangeException"></exception>
190+
public static IEnumerable<IEnumerable<T>> ChunkBy<T>(this IEnumerable<T> source, int size)
191+
{
192+
if (source == null) throw new ArgumentNullException(nameof(source));
193+
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size), "La dimensione deve essere maggiore di zero.");
194+
195+
return source.Select((x, i) => new { Index = i, Value = x })
196+
.GroupBy(x => x.Index / size)
197+
.Select(g => g.Select(x => x.Value));
198+
}
199+
200+
/// <summary>
201+
/// Get a random element from the list.
202+
/// </summary>
203+
/// <typeparam name="T"></typeparam>
204+
/// <param name="source"></param>
205+
/// <returns></returns>
206+
/// <exception cref="ArgumentNullException"></exception>
207+
/// <exception cref="InvalidOperationException"></exception>
208+
public static T RandomElement<T>(this IEnumerable<T> source)
209+
{
210+
if (source == null) throw new ArgumentNullException(nameof(source));
211+
var list = source.ToList();
212+
213+
if (list.Count == 0)
214+
throw new InvalidOperationException("La sequenza non contiene elementi.");
215+
216+
Random rng = new Random();
217+
int index = rng.Next(list.Count);
218+
return list[index];
219+
}
220+
221+
/// <summary>
222+
/// Check if all elements in the list are distinct.
223+
/// </summary>
224+
/// <typeparam name="T"></typeparam>
225+
/// <param name="source"></param>
226+
/// <returns></returns>
227+
/// <exception cref="ArgumentNullException"></exception>
228+
public static bool AllDistinct<T>(this IEnumerable<T> source)
229+
{
230+
if (source == null) throw new ArgumentNullException(nameof(source));
231+
232+
var seen = new HashSet<T>();
233+
foreach (var item in source)
234+
{
235+
if (!seen.Add(item))
236+
return false;
237+
}
238+
return true;
239+
}
240+
241+
/// <summary>
242+
/// Shuffle the list.
243+
/// </summary>
244+
/// <typeparam name="T"></typeparam>
245+
/// <param name="source"></param>
246+
/// <returns></returns>
247+
/// <exception cref="ArgumentNullException"></exception>
248+
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
249+
{
250+
if (source == null) throw new ArgumentNullException(nameof(source));
251+
252+
Random rng = new Random();
253+
return source.OrderBy(_ => rng.Next());
254+
}
255+
256+
/// <summary>
257+
/// Sum the elements of the list.
258+
/// </summary>
259+
/// <typeparam name="T"></typeparam>
260+
/// <param name="source"></param>
261+
/// <param name="selector"></param>
262+
/// <returns></returns>
263+
/// <exception cref="ArgumentNullException"></exception>
264+
public static int Sum<T>(this IEnumerable<T> source, Func<T, int> selector)
265+
{
266+
if (source == null) throw new ArgumentNullException(nameof(source));
267+
if (selector == null) throw new ArgumentNullException(nameof(selector));
268+
269+
return source.Select(selector).Sum();
270+
}
151271
}
152272
}

0 commit comments

Comments
 (0)