forked from leofun01/Transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerableExtension.cs
35 lines (32 loc) · 978 Bytes
/
EnumerableExtension.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
using System;
using System.Collections.Generic;
namespace DotNetTransformer.Extensions {
public static class EnumerableExtension
{
public static bool Exist<T>(this IEnumerable<T> collection, Predicate<T> match) {
foreach(T item in collection)
if(match(item))
return true;
return false;
}
public static bool All<T>(this IEnumerable<T> collection, Predicate<T> match) {
return !collection.Exist<T>(e => !match(e));
}
public static T CollectAll<T>(this IEnumerable<T> collection, Func<T> func) {
T result = default(T);
foreach(T item in collection)
result = func(result, item);
return result;
}
public static IEnumerable<T> GetRange<T>(this T start, Predicate<T> match, Generator<T> next) {
T t = start;
do {
yield return t;
t = next(t);
} while(match(t));
}
public static IEnumerable<T> GetRange<T>(this T start, T stop, Generator<T> next) {
return GetRange<T>(start, t => !t.Equals(stop), next);
}
}
}