Macros are nifty

I have been trying to wrap my head around Hy macros and have made just a little bit progress. The concept is really powerful, but since it is so new to me, it is hard to find out where to actually use them. It seems that they are well suited on extending the language by building constructs that would be tedious to type or which would reflect on the problem domain better. In any case, I tinkered with them again and came up with not-so-useful infix macro. It allows the user to write code using infix notation, where the operator is in the middle, instead of in the beginning:

(defmacro infix [code]
  (quasiquote (
    (unquote (get code 1))
    (unquote (get code 0))
    (unquote (get code 2)))))

With this macro, one can write code like:

=> (infix (1 + 1))

Not very useful, but still nice little exercise in macro writing.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s