Repository: yasufumy/pipelib

Issue:
Add Dataset.window method
[FYI](https://github.com/pytoolz/toolz/blob/master/toolz/itertoolz.py#L662)

Discussion:

Patch:
diff --git a/pipelib/core.py b/pipelib/core.py
index 7fbce5b..a55b986 100644
--- a/pipelib/core.py
+++ b/pipelib/core.py
@@ -1,7 +1,8 @@
 import random
 import pickle
 from pathlib import Path
-from itertools import chain, islice
+from itertools import chain, islice, tee
+from collections import deque
 
 import pipelib
 
@@ -39,6 +40,12 @@ class Dataset:
                 yield from chunk
         return PipelinedDataset(self, f)
 
+    def window(self, window_size):
+        def f(dataset):
+            yield from zip(*(deque(islice(it, i), 0) or it
+                           for i, it in enumerate(tee(dataset, window_size))))
+        return PipelinedDataset(self, f)
+
     def map(self, map_func):
         def f(dataset):
             return map(map_func, dataset)


Test patch:
diff --git a/tests/test_core.py b/tests/test_core.py
index a9cc5e1..92f5e58 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -60,6 +60,16 @@ class DatasetTestCase(TestCase):
         self.check_for_loop(chain.from_iterable(data), self.base)
         self.check_correct_pipelined_dataset(data, self.base, nested=False)
 
+    def test_window(self):
+        window_size = 3
+        data = self.data.window(window_size)
+        for x in data:
+            self.assertEqual(len(x), window_size)
+
+        expected = zip(*(self.base[i:] for i in range(window_size)))
+        self.check_for_loop(data, expected)
+        self.check_correct_pipelined_dataset(data, self.base, nested=False)
+
     def test_map(self):
         def f(x):
             return x ** 2