Skip to content
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

Language feature: list, set, dict comprehensions #6

Open
tusharsadhwani opened this issue Aug 16, 2023 · 5 comments
Open

Language feature: list, set, dict comprehensions #6

tusharsadhwani opened this issue Aug 16, 2023 · 5 comments

Comments

@tusharsadhwani
Copy link
Owner

Current implementation supports lists, sets and dicts, but it doesn't support their comprehensions.

Code like:

my_list = [i*2 for i in range(10)]
my_set = {i*j for i in range(10) for j in range(10)}
my_dict = {i: i*2 for i in range(10) if i % 2 == 0}
@Jacco
Copy link
Contributor

Jacco commented Nov 29, 2023

Did this one too :-) Will make a PR.

@Jacco
Copy link
Contributor

Jacco commented Dec 3, 2023

I'm already making good progress with the dict and list comprehensions. I ran into the fact that the list comprehensions can be repeated:

            text = (("Hi", "Steve!"), ("What's", "up?"))
            print([word for sentence in text for word in sentence])

I checked the ast python generates:

Module(
   body=[
      Assign(
         targets=[
            Name(id='text', ctx=Store())],
         value=Tuple(
            elts=[
               Tuple(
                  elts=[
                     Constant(value='Hi'),
                     Constant(value='Steve!')],
                  ctx=Load()),
               Tuple(
                  elts=[
                     Constant(value="What's"),
                     Constant(value='up?')],
                  ctx=Load())],
            ctx=Load())),
      Expr(
         value=ListComp(
            elt=Name(id='word', ctx=Load()),
            generators=[
               comprehension(
                  target=Name(id='sentence', ctx=Store()),
                  iter=Name(id='text', ctx=Load()),
                  ifs=[],
                  is_async=0),
               comprehension(
                  target=Name(id='word', ctx=Store()),
                  iter=Name(id='sentence', ctx=Load()),
                  ifs=[],
                  is_async=0)]))],
   type_ignores=[])

My problem is that I am struggling to make the interpreter work with the generators list in ListComp. Probably I can do it in the non-lazy way but there must be a better way :-)

@tusharsadhwani

@tusharsadhwani
Copy link
Owner Author

What problem are you facing with the generators list? For list and dict comprehensions we shouldn't need generators just yet.

@Jacco
Copy link
Contributor

Jacco commented Dec 4, 2023

For the case

[word for sentence in text for word in sentence]

The first generator is [for sentence in text]
The second generator is [for word in sentence]

If the interpreter handles this correctly it should:

  1. get an item from the first generator
  2. yield items from the item fetched until those are finished then repeat step 1

Now two 2 generators this might seem easy but what if there are 3 or 4 etc.

I am not sure how to implement that.

@tusharsadhwani
Copy link
Owner Author

tusharsadhwani commented Dec 15, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants