diff --git a/iterpy/arr.py b/iterpy/arr.py index e9d44a1..9a56fd3 100644 --- a/iterpy/arr.py +++ b/iterpy/arr.py @@ -26,6 +26,8 @@ def __len__(self) -> int: return len(self._iter) def __bool__(self) -> bool: + if self.len() == 0: + return False return self.lazy().__bool__() def __iter__(self) -> Arr[T]: diff --git a/iterpy/test_arr.py b/iterpy/test_arr.py index ed04d67..0fbe94e 100644 --- a/iterpy/test_arr.py +++ b/iterpy/test_arr.py @@ -161,6 +161,15 @@ def test_chain(): assert result == [1, 2, 3, 4] +def test_empty_is_falsy(): + test_iterator = Arr([]) # type: ignore + assert not test_iterator + + test_iterator = Arr([1]) + assert test_iterator + assert bool(test_iterator) is True + + @pytest.mark.benchmark() def test_benchmark_large_flattening(): test_input = Arr(range(100_000)).map(lambda x: Arr([x]))