-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
37 lines (33 loc) · 884 Bytes
/
Extensions.cs
File metadata and controls
37 lines (33 loc) · 884 Bytes
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
namespace Test.Services;
/// <summary>
/// Utility extensions.
/// </summary>
public static class Extensions
{
/// <summary>
/// Ammeds enumerable with context information about
/// index and previous and following items.
/// </summary>
/// <typeparam name="T">A source item type.</typeparam>
/// <param name="source">A source enumerable.</param>
/// <returns>Enumerable with context.</returns>
public static IEnumerable<(T current, int index, T? prev, T? next)> WithContext<T>(this IEnumerable<T> source)
{
T? prev = default;
T? current = default;
var index = -1;
foreach(var item in source)
{
if (index >= 0)
{
yield return (current!, index, prev, item);
}
++index;
(prev, current) = (current, item);
}
if (index >= 0)
{
yield return (current!, index, prev, default);
}
}
}