Skip to content

Commit 30f052e

Browse files
Merge pull request #7 from brandonwillard/use-custom-getitem
Make cdr use custom __getitem__ methods
2 parents fc2bc07 + cb5c2f0 commit 30f052e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

cons/core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515

1616
def rest(seq):
17-
if isinstance(seq, Iterator) and length_hint(seq, 2) <= 1:
17+
if isinstance(seq, Sequence):
18+
return seq[1:]
19+
elif isinstance(seq, Iterator) and length_hint(seq, 2) <= 1:
1820
return iter([])
19-
else:
20-
return islice(seq, 1, None)
21+
22+
return islice(seq, 1, None)
2123

2224

2325
class ConsError(ValueError):

tests/test_cons.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ def test_car_cdr():
209209
assert car(cons(1, cons("a", "b"))) == 1
210210
assert cdr(cons(1, cons("a", "b"))) == cons("a", "b")
211211

212+
# We need to make sure that `__getitem__` is actually used.
213+
from collections import UserList
214+
215+
class CustomList(UserList):
216+
def __getitem__(self, *args):
217+
return [5]
218+
219+
assert cdr(CustomList([1, 2, 3])) == [5]
220+
212221

213222
def test_unification():
214223
car_lv, cdr_lv = var(), var()

0 commit comments

Comments
 (0)