Skip to content
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

✨ Add convert to numeric stream operation #54

Merged
merged 2 commits into from
Jul 25, 2023
Merged
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
4 changes: 4 additions & 0 deletions pystreamapi/_streams/__base_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ def __map_to_str(self):
"""Converts the stream to strings."""
self._map(str)

def numeric(self) -> NumericBaseStream:
"""Returns a numeric stream. If the stream is already numeric, it is returned."""
return self._to_numeric_stream()

@_operation
def parallel(self) -> 'ParallelStream[K]':
"""Returns a parallel stream. If the stream is already parallel, it is returned."""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_stream_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def test_map_to_str(self):
result = self.stream([1, 2, 3, 9]).map_to_str().to_list()
self.assertListEqual(result, ["1", "2", "3", "9"])

def test_convert_to_numeric_stream(self):
result = self.stream([1, 2, 3, 9]).numeric()
self.assertTrue(isinstance(result, NumericBaseStream))

def test_convert_to_numeric_stream_is_already_numeric(self):
result = self.stream([1.0, 2.0, 3.0, 9.0]).numeric()
self.assertTrue(isinstance(result, NumericBaseStream))

def test_flat_map(self):
result = self.stream([1, 2, 3, 9]).flat_map(lambda x: self.stream([x, x])).to_list()
self.assertListEqual(result, [1, 1, 2, 2, 3, 3, 9, 9])
Expand Down
Loading