Skip to content

Commit d3c031a

Browse files
Remove unnecessary rest function
1 parent 30f052e commit d3c031a

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

cons/core.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
# from toolz.itertoolz import rest
1414

1515

16-
def rest(seq):
17-
if isinstance(seq, Sequence):
18-
return seq[1:]
19-
elif isinstance(seq, Iterator) and length_hint(seq, 2) <= 1:
20-
return iter([])
21-
22-
return islice(seq, 1, None)
23-
24-
2516
class ConsError(ValueError):
2617
pass
2718

@@ -259,14 +250,18 @@ def cdr(z):
259250
def _cdr(z):
260251
if len(z) == 0:
261252
raise ConsError("Not a cons pair")
262-
return type(z)(rest(z))
253+
return z[1:]
263254

264255

265256
@_cdr.register(Iterator)
266257
def _cdr_Iterator(z):
267258
if length_hint(z, 1) == 0:
268259
raise ConsError("Not a cons pair")
269-
return rest(z)
260+
261+
if isinstance(z, Iterator) and length_hint(z, 2) <= 1:
262+
return iter([])
263+
264+
return islice(z, 1, None)
270265

271266

272267
@_cdr.register(OrderedDict)

tests/test_cons.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from unification import unify, reify, var
99

1010
from cons import cons, car, cdr
11-
from cons.core import ConsPair, MaybeCons, ConsNull, rest, ConsError, NonCons
11+
from cons.core import ConsPair, MaybeCons, ConsNull, ConsError, NonCons
1212

1313

1414
def assert_all_equal(*tests):
@@ -54,7 +54,6 @@ def test_cons_type():
5454
assert not isinstance("hi", ConsPair)
5555
assert not isinstance(1, ConsPair)
5656
assert not isinstance(iter([]), ConsPair)
57-
assert not isinstance(rest(iter([1])), ConsPair)
5857
assert not isinstance(OrderedDict({}), ConsPair)
5958
assert not isinstance((), ConsPair)
6059
assert not isinstance([], ConsPair)
@@ -70,7 +69,6 @@ def test_cons_null():
7069
assert isinstance(tuple(), ConsNull)
7170
assert isinstance(OrderedDict(), ConsNull)
7271
assert isinstance(iter([]), ConsNull)
73-
assert isinstance(rest(iter([1])), ConsNull)
7472
assert not isinstance(object, ConsNull)
7573
assert not isinstance([1], ConsNull)
7674
assert not isinstance((1,), ConsNull)
@@ -212,11 +210,14 @@ def test_car_cdr():
212210
# We need to make sure that `__getitem__` is actually used.
213211
from collections import UserList
214212

213+
# Also, make sure `cdr` returns the `__getitem__` result unaltered
214+
clist_res = [5]
215+
215216
class CustomList(UserList):
216217
def __getitem__(self, *args):
217-
return [5]
218+
return clist_res
218219

219-
assert cdr(CustomList([1, 2, 3])) == [5]
220+
assert cdr(CustomList([1, 2, 3])) is clist_res
220221

221222

222223
def test_unification():

0 commit comments

Comments
 (0)