Skip to content

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