-
Notifications
You must be signed in to change notification settings - Fork 13
Add a flatten Python goody
#370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| def flatten(self: list[Any]) -> Generator[Any, None, None]: | ||
| for i in self: | ||
| if isinstance(i, list): | ||
| yield from flatten(i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I like it.
One thing to consider: this flattens one level of the input however I believe in many libraries flatten recursively flattens nested lists as well, or at least allow you to configure how many levels of nesting to flatten. I'm cool with starting with this, but it would be nice to allow for deeper flattening in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this code does recursively flatten nested lists to a single layer.
Would we still want to configure how many layers it flattens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, I missed that for some reason. Yeah, I think it might be nice to configure. JavaScript supports this (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) as does Ruby (https://apidock.com/ruby/Array/flatten).
But let's do that in a follow-up PR since this one is already working well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created #387.
| @@ -0,0 +1,9 @@ | |||
| from typing import Generator, Any | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other typing systems I've seen there's usually a safe version of "I don't know what this is" and an unsafe one. For example in TypeScript any is the unsafe version, because the typechecker will let you do anything with an any whereas unknown is the safe one, because the typechecker won't really let you do anything with an unknown value until you've checked if it's an instance of some more specific type.
I'm not as familiar with Python typing, so I'm not sure whether Any is considered evil or not. Is there another way of saying "I don't know what this is"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any is Evil in python too. I made it better now.
Closes #341.