-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnumerableExtensions.cs
executable file
·142 lines (128 loc) · 5.08 KB
/
EnumerableExtensions.cs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace UberTools
{
public static class EnumerableExtensions
{
// Comparison delegate for various functions
public delegate T Comparer<T>(T obj1, T obj2);
// Filter predicate for various functions
public delegate bool Predicate<T>(T obj);
// Applies 'action' to all items in 'items'
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (T item in items)
{
action(item);
}
}
// Returns a random element from 'items'
public static T RandomChoice<T>(this IEnumerable<T> items)
{
return items.ToList().RandomChoice();
}
/// Finds the 'best' match in the IEnumerable.
/// Uses 'filter' to filter out unwanted results, and 'comparer' to find the best
/// If anything is found, returns true
/// Works with value or reference types
static public bool GetBest<T>(this IEnumerable<T> items, Predicate<T> filter, Comparer<T> comparer, ref T bestItem)
{
bool foundOne = false;
foreach (var item in items)
{
if (filter(item))
{
if (!foundOne)
{
bestItem = item;
foundOne = true;
}
else
{
bestItem = comparer(bestItem, item);
}
}
}
return foundOne;
}
/// Finds the 'best' match in the IEnumerable.
/// Uses 'filter' to filter out unwanted results, and 'comparer' to find the best
/// If anything is found, returns that item
/// Works only with reference types
static public T GetBest<T>(this IEnumerable<T> items, Predicate<T> filter, Comparer<T> comparer) where T : class
{
T bestItem = null;
GetBest(items, filter, comparer, ref bestItem);
return bestItem;
}
/// Finds the 'best' match in the IEnumerable.
/// Goes through all the items in 'items' and uses 'comparer' to find the best
/// If anything is found, returns true
/// Works with value or reference types
static public bool GetBest<T>(this IEnumerable<T> items, Comparer<T> comparer, ref T bestItem)
{
bool foundOne = false;
foreach (var item in items)
{
if (!foundOne)
{
bestItem = item;
foundOne = true;
}
else
{
bestItem = comparer(bestItem, item);
}
}
return foundOne;
}
/// Finds the 'best' match in the IEnumerable.
/// Goes through all the items in 'items' and uses 'comparer' to find the best
/// If anything is found, returns that item
/// Works with reference types
static public T GetBest<T>(this IEnumerable<T> items, Comparer<T> comparer) where T : class
{
T bestItem = null;
GetBest(items, comparer, ref bestItem);
return bestItem;
}
// Finds the first item in 'items' that passes 'filter'
// Returns 'true' if it finds anything
// Works with value or reference types
static public bool GetFirst<T>(this IEnumerable<T> items, Predicate<T> filter, ref T foundItem)
{
foreach (var item in items)
{
if (filter(item))
{
foundItem = item;
return true;
}
}
return false;
}
// Finds the first item in 'items' that passes 'filter'
// Returns the item found, or null
// Works with only reference types
static public T GetFirst<T>(this IEnumerable<T> items, Predicate<T> filter) where T : class
{
T foundItem = null;
GetFirst(items, filter, ref foundItem);
return foundItem;
}
// Generator that returns all items in 'items' matching 'filter'
// Works with value or reference types
static public IEnumerable<T> GetMatches<T>(this IEnumerable<T> items, Predicate<T> filter)
{
foreach (var item in items)
{
if (filter(item))
{
yield return item;
}
}
}
}
}