Last active
August 25, 2023 09:22
-
-
Save hzhu212/0c1827dc1188844a1d92394e2d1cea05 to your computer and use it in GitHub Desktop.
Revisions
-
hzhu212 revised this gist
Dec 11, 2020 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,6 +51,14 @@ class Stream(object): fun = lambda x: x self._iterable = (x for x in self._iterable if fun(x)) return self def stopwhen(self, fun): def gen(): for x in self._iterable: if fun(x): break yield x return Stream(gen()) def reduce(self, fun, initial=None): return functools.reduce(fun, self._iterable, initial) -
hzhu212 revised this gist
Nov 27, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,7 @@ class Stream(object): def reduce(self, fun, initial=None): return functools.reduce(fun, self._iterable, initial) def foreach(self, fun): for x in self._iterable: fun(x) -
hzhu212 revised this gist
Nov 22, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ Python Stream object act like Java Stream, features include lazy calculating, slicing, chain-call etc. `stream.py`: ```python3 -
hzhu212 renamed this gist
Nov 22, 2020 . 1 changed file with 23 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,10 @@ `stream.py`: ```python3 import functools import itertools class Stream(object): """ a stream object for chain-call @@ -37,17 +39,34 @@ class Stream(object): def apply(self, fun, *args, **kwargs): self._iterable = (fun(x, *args, **kwargs) for x in self._iterable) return self def map(self, fun): self._iterable = (fun(x) for x in self._iterable) return self def filter(self, fun=None): if fun is None: fun = lambda x: x self._iterable = (x for x in self._iterable if fun(x)) return self def reduce(self, fun, initial=None): return functools.reduce(fun, self._iterable, initial) def for_each(self, fun): for x in self._iterable: fun(x) def collect(self): return list(self._iterable) ``` Use cases: ```python In [1]: Stream(range(100)).skip(10).step(3).limit(10).map(lambda x: x//3).collect() Out[1]: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [2]: Stream(range(100))[10:20:3].map(lambda x: x+1).collect() Out[2]: [11, 14, 17, 20] ``` -
hzhu212 revised this gist
Nov 13, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,8 +45,8 @@ class Stream(object): Use cases: ```python In [1]: Stream(range(100)).skip(10).step(3).limit(10).apply(lambda x: x//3).tolist() Out[1]: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [2]: Stream(range(100))[10:20:3].apply(lambda x: x+1).tolist() Out[2]: [11, 14, 17, 20] -
hzhu212 revised this gist
Nov 13, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ class Stream(object): Use cases: ```python In [15]: Stream(range(100)).skip(10).step(3).limit(10).apply(lambda x: x//3).tolist() Out[15]: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] -
hzhu212 revised this gist
Nov 13, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ class Stream(object): Use cases: ```ipython In [15]: Stream(range(100)).skip(10).step(3).limit(10).apply(lambda x: x//3).tolist() Out[15]: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] -
hzhu212 created this gist
Nov 13, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ `stream.py`: ```python3 import itertools class Stream(object): """ a stream object for chain-call """ def __init__(self, iterable): self._iterable = iterable def __iter__(self): return self def __next__(self): return next(self._iterable) def skip(self, n): self._iterable = itertools.islice(self._iterable, n, None, 1) return self def limit(self, n): self._iterable = itertools.islice(self._iterable, None, n, 1) return self def step(self, n): self._iterable = itertools.islice(self._iterable, None, None, n) return self def __getitem__(self, slc): if not isinstance(slc, slice): raise ValueError(f'"[]" operator supports only slice object, but {slc!r} got') self._iterable = itertools.islice(self._iterable, slc.start, slc.stop, slc.step) return self def apply(self, fun, *args, **kwargs): self._iterable = (fun(x, *args, **kwargs) for x in self._iterable) return self def tolist(self): return list(self) ``` Use cases: ``` In [15]: Stream(range(100)).skip(10).step(3).limit(10).apply(lambda x: x//3).tolist() Out[15]: [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [2]: Stream(range(100))[10:20:3].apply(lambda x: x+1).tolist() Out[2]: [11, 14, 17, 20] ```