Skip to content
Open
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 @@ -90,6 +90,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