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

Avoid duplicate evaluations in chaining comparison #327

Closed
yuanming-hu opened this issue Dec 28, 2019 · 3 comments
Closed

Avoid duplicate evaluations in chaining comparison #327

yuanming-hu opened this issue Dec 28, 2019 · 3 comments
Assignees

Comments

@yuanming-hu
Copy link
Member

What's the behavior of 1 < ti.append(x, [], 1) < 10?

@yuanming-hu yuanming-hu self-assigned this Dec 28, 2019
@archibate
Copy link
Collaborator

Behaves bad:

>>> 1 < 233 < 10
False

You may want to define a function, whose parameter holds temp value:

def between(a, x, b):
  return a < x and x < b

Or try ti.append(x, [], 1) in range(0, 10) if integer for elegence.

@yuanming-hu
Copy link
Member Author

Yeah, call a function is a good solution as the result of ti.append is now passed by value instead of getting evaluated twice. Given that the chain can be rather long (e.g. a < b < c < d < e), it's better to modify the frontend compiler to cache the results of comparators:

def visit_Compare(self, node):

Assigning this to @xumingkuan since only a little bit of Python engineering is needed.

@yuanming-hu
Copy link
Member Author

Well done by @xumingkuan. Closing.

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

No branches or pull requests

3 participants