Skip to content

Instantly share code, notes, and snippets.

@kokudori
Last active December 15, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kokudori/5314509 to your computer and use it in GitHub Desktop.
Save kokudori/5314509 to your computer and use it in GitHub Desktop.
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)));
}
}
@kokudori
Copy link
Author

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