-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCoroutineCombinator.cs
41 lines (38 loc) · 1.08 KB
/
CoroutineCombinator.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
// https://github.com/noseratio/coroutines-talk
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
namespace Coroutines
{
public static class CoroutineCombinator<T>
{
public static IEnumerable<T> Combine(params Func<IEnumerable<T>>[] coroutines)
{
var list = coroutines.Select(c => c().GetEnumerator()).ToList();
try
{
while (list.Count > 0)
{
for (var i = 0; i < list.Count; i++)
{
var coroutine = list[i];
if (coroutine.MoveNext())
{
yield return coroutine.Current;
}
else
{
coroutine.Dispose();
list.RemoveAt(i);
}
}
}
}
finally
{
list.ForEach(c => c.Dispose());
}
}
}
}