Skip to content

Commit 199fe41

Browse files
committed
use rev in llist(); update comment
1 parent 2df29d9 commit 199fe41

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

unpythonic/llist.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from unpythonic.fun import composer1i
1212
from unpythonic.fold import foldr, foldl
13+
from unpythonic.it import rev
1314

1415
# explicit list better for tooling support
1516
_exports = ["cons", "nil",
@@ -284,17 +285,13 @@ def llist(iterable):
284285
285286
- Sequences, since they can be walked backwards; a linear walk is enough.
286287
287-
For a general iterable input, this costs a linear walk (forwards), plus an
288-
``lreverse`` once the list has been fully consed.
288+
For a general iterable input, this costs a linear walk (forwards),
289+
plus an ``lreverse``.
289290
"""
290291
if isinstance(iterable, LinkedListReverseIterator):
291292
# avoid two extra reverses by reusing the internal data.
292293
return iterable._data
293-
try: # maybe a sequence?
294-
return lreverse(reversed(iterable))
295-
except TypeError:
296-
reversed_as_ll = lreverse(iterable)
297-
return lreverse(reversed_as_ll)
294+
return lreverse(rev(iterable))
298295

299296
def lreverse(iterable):
300297
"""Reverse an iterable, loading the result into a linked list.
@@ -438,8 +435,7 @@ def test():
438435
r = reversed(ll(1, 2, 3)) # an iterator that internally builds the reversed list...
439436
assert llist(r) is r._data # ...which llist should just grab
440437

441-
# With the real recursive-process foldr, this no longer requires reversed(),
442-
# but is prone to call stack overflow.
438+
# foldr implicitly reverses the input
443439
assert foldr(cons, nil, ll(1, 2, 3)) == ll(1, 2, 3)
444440

445441
print("All tests PASSED")

0 commit comments

Comments
 (0)