Skip to content

add running #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ zeros: Stream[int] = (
assert list(zeros) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
```

### "running map"

> {TODO: add description}

```python
from streamable import running

cumulative_sum: Stream[int] = (
integers
.map(running(lambda cumsum, i: cumsum + i, initial=0))
)

assert list(cumulative_sum) == [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]
```



## `.foreach`
Expand Down
2 changes: 1 addition & 1 deletion streamable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from streamable.stream import Stream
from streamable.util.functiontools import star
from streamable.util.functiontools import running, star
14 changes: 14 additions & 0 deletions streamable/util/functiontools.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ def add(a: int, b: int) -> int:
```
"""
return _Star(func)


def running(func: Callable[[R, T], R], initial: R) -> Callable[[T], R]:
"""
TODO
"""
acc = initial

def _(elem: T) -> R:
nonlocal acc
acc = func(acc, elem)
return acc

return _
10 changes: 10 additions & 0 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ def test_starmap_example(self) -> None:

assert list(zeros) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

def test_running_map_example(self) -> None:
from streamable import running

cumulative_sum: Stream[int] = (
integers
.map(running(lambda cumsum, i: cumsum + i, initial=0))
)

assert list(cumulative_sum) == [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

def test_foreach_example(self) -> None:
state: List[int] = []
appending_integers: Stream[int] = integers.foreach(state.append)
Expand Down
Loading