Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does Hy have inline if?

What is the Hy equivalent of Python’s a if condition else b?
I’m trying to convert something like this

return (quicksort(under) if under else []) + same + (quicksort(over) if over else [])

to Hy. It avoids calling quicksort() if the list is empty. I know I can do

(if under
   (quicksort under)
   [])

but I’d rather have it on one line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Hy is a free-form language (like most programming languages, but unlike Python). You can write (if under (quicksort under) []) in one line, like so, and it makes no difference to the parser.

Whether the Hy compiler produces a Python if expression or a Python if statement for your Hy if form is supposed to be an implementation detail that you don’t have to worry about.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading