Search code examples
pythonlisphy

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


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.