Create a gist now

Instantly share code, notes, and snippets.

@kokudori /Buffer.cs
Last active Dec 15, 2015

What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
static class Extentions
{
public static IEnumerable<IEnumerable<T>> Buffer<T>(this IEnumerable<T> source, Func<T, bool> func)
{
while (source.Any())
{
yield return source.TakeWhile(x => !func(x));
source = source.SkipWhile(x => !func(x)).Skip(1);
}
}
}
class Program
{
static void Main(string[] args)
{
var nums = new[] { 1, 2, -1, 5, 6, 8, -1, 11, 2, 4, 6, -1, 9 };
nums.Buffer(x => x == -1).ForEach(x => Console.WriteLine(string.Join(",\t", x)));
}
}
Owner

kokudori commented Apr 4, 2013

output:

1,      2
5,      6,      8
11,     2,      4,      6
9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment