LINQでBufferしたい
Observable.BufferをLINQで実現する
var arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/* GroupByを使う */
IEnumerable<IEnumerable<int>> arr2 = arr.Select((value, index) => (value, index)).GroupBy(a => a.index / 3).Select(a => a.Select(b => b.value));
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
/* Aggregateを使う */
List<List<int>> arr3 = arr.Aggregate(new List<List<byte>>(), (list, next) =>
{
if (!list.Any() || list.Last().Count() >= 3) list.Add(new List<byte>());
list.Last().Add(next);
return list;
});
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
/* Observable.Bufferを使う */
IList<IList<int>> arr4 = await arr.ToObservable().Buffer(3).ToList().ToTask();
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
コメント
コメントを投稿