internal class Program
{
static async Task Main(string[] args)
{
var list = new List<int>()
{
1, 2, 3, 4, 5, 6, 7,
};
var res = Pairwise<int, string>(list, (s1, s2) =>
{
return $"{s1}:{s2}";
});
foreach (var item in res)
{
Console.WriteLine(item);
}
}
public static IEnumerable<TResult> Pairwise<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, TSource, TResult> resultSelector)
{
using var e = source.GetEnumerator();
if (!e.MoveNext()) yield break;
var previous = e.Current;
while (e.MoveNext())
{
yield return resultSelector(previous, e.Current);
previous = e.Current;
}
}
}
输出