forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableHelpers.cs
More file actions
72 lines (63 loc) · 2.01 KB
/
Copy pathEnumerableHelpers.cs
File metadata and controls
72 lines (63 loc) · 2.01 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
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Microsoft.ClearScript.Util
{
internal static class EnumerableHelpers
{
public static readonly HostType HostType = HostType.Wrap(typeof(EnumerableHelpers));
public static IList<T> ToIList<T>(this IEnumerable<T> source)
{
return (source as IList<T>) ?? source.ToList();
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var element in source)
{
action(element);
}
}
public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
{
var index = 0;
foreach (var element in source)
{
action(element, index++);
}
}
public static IEnumerable<string> ExcludeIndices(this IEnumerable<string> names)
{
foreach (var name in names)
{
int index;
if (!int.TryParse(name, NumberStyles.Integer, CultureInfo.InvariantCulture, out index))
{
yield return name;
}
}
}
public static IEnumerable<int> GetIndices(this IEnumerable<string> names)
{
foreach (var name in names)
{
int index;
if (int.TryParse(name, NumberStyles.Integer, CultureInfo.InvariantCulture, out index))
{
yield return index;
}
}
}
public static IEnumerator<T> GetEnumerator<T>(IEnumerable<T> source)
{
return source.GetEnumerator();
}
public static IEnumerator GetEnumerator(IEnumerable source)
{
return source.GetEnumerator();
}
}
}