Skip to content

Instantly share code, notes, and snippets.

@hzhu212
Last active August 25, 2023 09:22
Show Gist options
  • Save hzhu212/0c1827dc1188844a1d92394e2d1cea05 to your computer and use it in GitHub Desktop.
Save hzhu212/0c1827dc1188844a1d92394e2d1cea05 to your computer and use it in GitHub Desktop.

Revisions

  1. hzhu212 revised this gist Dec 11, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions python_stream.md
    Original 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)
  2. hzhu212 revised this gist Nov 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion python_stream.md
    Original 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 for_each(self, fun):
    def foreach(self, fun):
    for x in self._iterable:
    fun(x)

  3. hzhu212 revised this gist Nov 22, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions python_stream.md
    Original 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
  4. hzhu212 renamed this gist Nov 22, 2020. 1 changed file with 23 additions and 4 deletions.
    27 changes: 23 additions & 4 deletions iterator_chain_call.md → python_stream.md
    Original 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 tolist(self):
    return list(self)
    def collect(self):
    return list(self._iterable)
    ```

    Use cases:

    ```python
    In [1]: Stream(range(100)).skip(10).step(3).limit(10).apply(lambda x: x//3).tolist()
    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].apply(lambda x: x+1).tolist()
    In [2]: Stream(range(100))[10:20:3].map(lambda x: x+1).collect()
    Out[2]: [11, 14, 17, 20]
    ```
  5. hzhu212 revised this gist Nov 13, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions iterator_chain_call.md
    Original file line number Diff line number Diff line change
    @@ -45,8 +45,8 @@ 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]
    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]
  6. hzhu212 revised this gist Nov 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion iterator_chain_call.md
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ class Stream(object):

    Use cases:

    ```ipython
    ```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]

  7. hzhu212 revised this gist Nov 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion iterator_chain_call.md
    Original 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]
  8. hzhu212 created this gist Nov 13, 2020.
    53 changes: 53 additions & 0 deletions iterator_chain_call.md
    Original 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]
    ```