diff --git a/pystreamapi/_streams/__base_stream.py b/pystreamapi/_streams/__base_stream.py index 16469dd..1545327 100644 --- a/pystreamapi/_streams/__base_stream.py +++ b/pystreamapi/_streams/__base_stream.py @@ -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.""" diff --git a/tests/test_stream_implementation.py b/tests/test_stream_implementation.py index a4453a3..71cafc9 100644 --- a/tests/test_stream_implementation.py +++ b/tests/test_stream_implementation.py @@ -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])