-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteEnumerable.cs
More file actions
36 lines (28 loc) · 1.56 KB
/
InfiniteEnumerable.cs
File metadata and controls
36 lines (28 loc) · 1.56 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
using System;
using System.Collections.Generic;
using System.Linq;
using Ed.Extensions.Enumerable;
using static Ed.Extensions.Enumerable.EnumerableExtensions;
namespace Ed.Utilities.Enumerable{
public static class InfiniteEnumerable{
public static IEnumerable<T> Inifinite<T>(Func<T> next) {while(true) yield return next() ;}
public static IEnumerable<T> Inifinite<T> (Func<T> next, Func<bool> stopCondition){
while(!stopCondition()) yield return next();
}
public static IEnumerable<T> Inifinite<T> (Func<T> next, Func<T,bool> stopCondition,bool includeLast){
var current = next();
while(!stopCondition(current)){
yield return current;
current = next();
}
if(includeLast)
yield return current;
}
public static IEnumerable<T> Inifinite<T>(T first, Func<T,T> next, bool includeFirst)
=> (includeFirst?first.Yield():Empty<T>()).Concat(Inifinite(()=> first = next(first) ));
public static IEnumerable<T> Inifinite<T>(T first, Func<T,T> next, bool includeFirst, Func<bool> stopCondition)
=> (includeFirst?first.Yield():Empty<T>()).Concat(Inifinite(()=> first = next(first),stopCondition ));
public static IEnumerable<T> Inifinite<T>(T first, Func<T,T> next, bool includeFirst, Func<T,bool> stopCondition, bool includeLast)
=> (includeFirst?first.Yield():Empty<T>()).Concat(Inifinite(()=> first = next(first), stopCondition ,includeLast ));
}
}