Skip to content

Conversation

@VolodymyrSemenov
Copy link
Contributor

@VolodymyrSemenov VolodymyrSemenov commented Aug 22, 2024

Closes #341.

@VolodymyrSemenov VolodymyrSemenov linked an issue Aug 22, 2024 that may be closed by this pull request
@VolodymyrSemenov VolodymyrSemenov self-assigned this Aug 22, 2024
@VolodymyrSemenov VolodymyrSemenov added python Adventure Pack Python issue enhancement New feature or request good first issue Good for newcomers labels Aug 22, 2024
@miorel miorel changed the title 341 add a flatten python goody Add a flatten python goody Aug 23, 2024
@miorel miorel changed the title Add a flatten python goody Add a flatten python goody Aug 23, 2024
def flatten(self: list[Any]) -> Generator[Any, None, None]:
for i in self:
if isinstance(i, list):
yield from flatten(i)
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

@miorel miorel Aug 26, 2024

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.

Copy link
Contributor

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
Copy link
Contributor

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"?

Copy link
Contributor Author

@VolodymyrSemenov VolodymyrSemenov Aug 24, 2024

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.

@miorel miorel merged commit 96d3794 into main Aug 26, 2024
@miorel miorel deleted the 341-add-a-flatten-python-goody branch August 26, 2024 02:54
@miorel miorel changed the title Add a flatten python goody Add a flatten Python goody Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request good first issue Good for newcomers python Adventure Pack Python issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a flatten Python goody

3 participants