|
10 | 10 |
|
11 | 11 | from unpythonic.fun import composer1i |
12 | 12 | from unpythonic.fold import foldr, foldl |
| 13 | +from unpythonic.it import rev |
13 | 14 |
|
14 | 15 | # explicit list better for tooling support |
15 | 16 | _exports = ["cons", "nil", |
@@ -284,17 +285,13 @@ def llist(iterable): |
284 | 285 |
|
285 | 286 | - Sequences, since they can be walked backwards; a linear walk is enough. |
286 | 287 |
|
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``. |
289 | 290 | """ |
290 | 291 | if isinstance(iterable, LinkedListReverseIterator): |
291 | 292 | # avoid two extra reverses by reusing the internal data. |
292 | 293 | 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)) |
298 | 295 |
|
299 | 296 | def lreverse(iterable): |
300 | 297 | """Reverse an iterable, loading the result into a linked list. |
@@ -438,8 +435,7 @@ def test(): |
438 | 435 | r = reversed(ll(1, 2, 3)) # an iterator that internally builds the reversed list... |
439 | 436 | assert llist(r) is r._data # ...which llist should just grab |
440 | 437 |
|
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 |
443 | 439 | assert foldr(cons, nil, ll(1, 2, 3)) == ll(1, 2, 3) |
444 | 440 |
|
445 | 441 | print("All tests PASSED") |
|
0 commit comments