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

Support guard clauses #100

Open
wd60622 opened this issue Nov 13, 2022 · 2 comments
Open

Support guard clauses #100

wd60622 opened this issue Nov 13, 2022 · 2 comments
Assignees
Labels
Milestone

Comments

@wd60622
Copy link

wd60622 commented Nov 13, 2022

Running in the colab notebook, noticed guard clauses are not supported in the cell rendering. Thought they are common enough, to get support

# works 
@latexify.function
def foo(x): 
    if x > 0: 
        return x
    else: 
        return 0 

foo 


@latexify.function 
def foo(x): 
    if x > 0: 
         return x

    return 0

# raises LatexifyNotSupportedError
foo

version: 0.2.0b2

@odashi
Copy link
Collaborator

odashi commented Nov 13, 2022

@wd60622
Thanks for raising the issue!
I think we can (relatively easily) support any functions in which:

  • All guard clauses have only a return statement or another if clause, and
  • All guard clauses are placed after any other statements, i.e., guard clauses can be converted to a pair of if-else.

Additionally, we can support any functions with only if clauses and assignments when reduce_assignments=True.

In the case of reduce_assignments=False (default), we need to consider how the discrepancy of assigned values should be expressed. For example:

def f(x):
    y = a(x)
    if b(x):
        return y
    y = c(x)
    return y

I think this kind of functions are not easily expressed without assignment reduction, and eventually we need the algorithmic form (#57)

@odashi odashi added the feature label Nov 13, 2022
@odashi odashi added this to the v0.3 milestone Nov 13, 2022
@odashi odashi self-assigned this Nov 13, 2022
@wd60622
Copy link
Author

wd60622 commented Nov 14, 2022

Awesome.
Yeah, It doesn't seem too hard of an adaption. Guessing without a guard, the orelse attribute of the ast.If will be the rest of the body in the case with a guard.

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

No branches or pull requests

2 participants