make HyCons iter return non-iterable cdr #1392
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This makes the HyCons
__iter__
generatorreturn
the last cdr when it's non-iterable. The old behavior was toyield
the cdr as the last element and raise aTypeError
.It seems more useful for a dotted list iter to return its cdr than yield it and then raise a
TypeError
like before. Ayield
should only be used on the cars, not the cdr. But we still want to be able to access the final cdr. This will make dotted lists easier to work with, since you will be able to use an iterator for them too. Before, you'd have to build one yourself with a while loop or recursion.I noticed this possibility in #1360, but the
__repr__
method still shouldn't use this, because a non-HyCons
cdr
needs to print differently, even if it's iterable. These are separate issues.On Python 3.3+, this would just be a
return self.cdr
statement. I explicitly raised theStopIteration
exception for compatibility with Python 2.7, which doesn't allowreturn
in generators at all. On Python3, the returned value is in the.value
attr of the exception. On Python2, it's still available in.args
, though I suppose we could explicitly set a.value
attr on the exception object before raising it.