Skip to content

Commit b403d59

Browse files
authored
Realize lazy seqs when checking if they are empty (#182)
1 parent a149e30 commit b403d59

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

basilisp/lang/seq.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def _realize(self):
182182
@property
183183
def is_empty(self) -> bool:
184184
if not self._realized:
185-
return False
185+
self._realize()
186+
return self.is_empty
186187
if self._seq is None or self._seq.is_empty:
187188
return True
188189
return False

tests/core_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@ def test_reduce():
537537
assert 46 == core.reduce(core.__PLUS__, 45, [1])
538538

539539

540+
def test_reduce_with_lazy_seq():
541+
assert 25 == core.reduce(core.__PLUS__, core.filter_(core.odd__Q__, vec.v(1, 2, 3, 4, 5, 6, 7, 8, 9)))
542+
assert 25 == core.reduce(core.__PLUS__, 0, core.filter_(core.odd__Q__, vec.v(1, 2, 3, 4, 5, 6, 7, 8, 9)))
543+
544+
540545
def test_comp():
541546
assert 1 == core.comp()(1)
542547
assert "hi" == core.comp()("hi")

tests/seq_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ def test_to_sequence():
1515

1616
def test_lazy_sequence():
1717
s = lseq.LazySeq(lambda: None)
18-
assert not s.is_empty, "LazySeq has not been realized yet"
18+
assert s.is_empty
1919
assert None is s.first
2020
assert lseq.EMPTY is s.rest
2121
assert s.is_realized
2222
assert s.is_empty, "LazySeq has been realized and is empty"
2323

2424
s = lseq.LazySeq(lambda: lseq.EMPTY)
25-
assert not s.is_empty, "LazySeq has not been realized yet"
25+
assert s.is_empty
2626
assert None is s.first
2727
assert lseq.EMPTY is s.rest
2828
assert s.is_realized
2929
assert s.is_empty, "LazySeq has been realized and is empty"
3030

3131
s = lseq.LazySeq(lambda: lseq.sequence([1]))
32-
assert not s.is_empty, "LazySeq has not been realized yet"
32+
assert not s.is_empty
3333
assert 1 == s.first
3434
assert lseq.EMPTY is s.rest
3535
assert s.is_realized
@@ -45,21 +45,21 @@ def inner_inner_seq():
4545
return lseq.LazySeq(inner_seq).cons(1)
4646

4747
s = lseq.LazySeq(lazy_seq)
48-
assert not s.is_empty, "LazySeq has not been realized yet"
48+
assert not s.is_empty
4949
assert 1 == s.first
5050
assert isinstance(s.rest, lseq.LazySeq)
5151
assert s.is_realized
5252
assert not s.is_empty, "LazySeq has been realized and is not empty"
5353

5454
r = s.rest
55-
assert not r.is_empty, "LazySeq has not been realized yet"
55+
assert not r.is_empty
5656
assert 2 == r.first
5757
assert isinstance(r.rest, lseq.LazySeq)
5858
assert r.is_realized
5959
assert not r.is_empty, "LazySeq has been realized and is not empty"
6060

6161
t = r.rest
62-
assert not t.is_empty, "LazySeq has not been realized yet"
62+
assert not t.is_empty
6363
assert 3 == t.first
6464
assert lseq.EMPTY is t.rest
6565
assert t.is_realized

0 commit comments

Comments
 (0)