BadScript 2
Loading...
Searching...
No Matches
EnumerableExtensions.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6
8{
9 internal static class EnumerableExtensions
10 {
11 public static int IndexOf<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
12 {
13 int index = -1;
14
15 foreach (TSource item in source)
16 {
17 index++;
18
19 if (predicate(item))
20 {
21 break;
22 }
23 }
24
25 return index;
26 }
27
28 public static object ToUntypedArray(this IEnumerable<object> value, Type type)
29 {
30 Array array = Array.CreateInstance(type, value.Count());
31
32 value.ToArray()
33 .CopyTo(array, 0);
34
35 return array;
36 }
37
38 public static bool Empty<TSource>(this IEnumerable<TSource> source)
39 {
40 return !source.Any();
41 }
42
54 public static IEnumerable<T[]> Group<T>(this IEnumerable<T> source, int groupSize)
55 {
56 if (groupSize < 1)
57 {
58 throw new ArgumentOutOfRangeException(nameof(groupSize));
59 }
60
61 T[] group = new T[groupSize];
62 int groupIndex = 0;
63
64 foreach (T item in source)
65 {
66 group[groupIndex++] = item;
67
68 if (groupIndex == groupSize)
69 {
70 yield return group;
71
72 group = new T[groupSize];
73 groupIndex = 0;
74 }
75 }
76 }
77 }
78}
static object ToUntypedArray(this IEnumerable< object > value, Type type)
static bool Empty< TSource >(this IEnumerable< TSource > source)
static int IndexOf< TSource >(this IEnumerable< TSource > source, Func< TSource, bool > predicate)
static IEnumerable< T[]> Group< T >(this IEnumerable< T > source, int groupSize)
Breaks a collection into groups of a specified size.