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

Add support for ternary conditional expression in tail call optimization #91

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

kiendang
Copy link

@kiendang kiendang commented Jan 25, 2019

This PR enables tail call optimization for ternary conditional expression in return statement.
e.g

@tco
def tail_sum(x, acc=0):
    return acc if len(x) == 0 else tail_sum(x[1:], acc + x[0])

@azazel75
Copy link
Collaborator

@kiendang thanks for the contribution, can you please add a test for this?.

@kiendang
Copy link
Author

I already did?

@kiendang
Copy link
Author

Sorry please don't merge yet. It's not perfect. I'll update + add more tests soon.

@kiendang
Copy link
Author

kiendang commented Jan 27, 2019

@azazel75 I've finished. Please review when you have time.

@tco
def tail_sum(x, acc=0):
    return acc if len(x) == 0 else tail_sum(x[1:], acc + x[0])

will now expand to

return (acc if len(x) == 0 else (TCOType.CALL, tail_sum, [x[1:], acc + x[0]], {}))

I also added support for non-return ternary expression in tail position and added more tests (basically all the existing tests for tco rewritten using ternary expression).

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

Successfully merging this pull request may close these issues.

2 participants