forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableExtensions.cs
More file actions
68 lines (59 loc) · 2.09 KB
/
Copy pathEnumerableExtensions.cs
File metadata and controls
68 lines (59 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommandLine.Infrastructure
{
static class EnumerableExtensions
{
public static int IndexOf<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
var index = -1;
foreach (var item in source)
{
index++;
if (predicate(item))
{
break;
}
}
return index;
}
public static object ToUntypedArray(this IEnumerable<object> value, Type type)
{
var array = Array.CreateInstance(type, value.Count());
value.ToArray().CopyTo(array, 0);
return array;
}
public static bool Empty<TSource>(this IEnumerable<TSource> source)
{
return !source.Any();
}
/// <summary>
/// Breaks a collection into groups of a specified size.
/// </summary>
/// <param name="source">A collection of <typeparam name="T"/>.</param>
/// <param name="groupSize">The number of items each group shall contain.</param>
/// <returns>An enumeration of T[].</returns>
/// <remarks>An incomplete group at the end of the source collection will be silently dropped.</remarks>
public static IEnumerable<T[]> Group<T>(this IEnumerable<T> source, int groupSize)
{
if (groupSize < 1)
{
throw new ArgumentOutOfRangeException(nameof(groupSize));
}
T[] group = new T[groupSize];
int groupIndex = 0;
foreach (var item in source)
{
group[groupIndex++] = item;
if (groupIndex == groupSize)
{
yield return group;
group = new T[groupSize];
groupIndex = 0;
}
}
}
}
}