Extension method for Enumerable.Intersperse?

前端 未结 5 1756
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 16:27

I learned the intersperse function from Haskell, and have been looking for an implementation in c#.

Intersperse takes 2 arguments, an IEnumerable source and

5条回答
  •  隐瞒了意图╮
    2020-12-11 17:05

    If you're wondering how to implement it, I would do so like this:

    public static IEnumerable Intersperse(this IEnumerable collection, T value)
    {
        foreach(T item in collection)
        {
            yield return item;
            yield return value;
        }
    
        yield break;
    }
    

提交回复
热议问题