;; Additional flow macros
(defmacro/g! guard [&rest args]
(setv g!cond (car args))
(setv g!body (car (cdr args)))
(setv g!rest (cdr (cdr args)))
(if g!rest
`(if ~g!cond
~g!body
(guard ~@g!rest))
`(if ~g!cond
~g!body)))
(defmacro/g! switch [variable &rest args]
(setv g!comp (car args))
(setv g!body (car (cdr args)))
(setv g!rest (cdr (cdr args)))
(setv g!cond `(~(car g!comp) ~variable ~@(cdr g!comp)))
(if g!rest
(if (cdr g!rest)
`(if ~g!cond ~g!body (switch ~variable ~@g!rest))
`(if ~g!cond ~g!body ~@g!rest))
`(if ~g!cond ~g!body)))
As you can see, switch is basically just a guard statement, but with the original variable playing a role in every comparison, and with an additional else clause (since guard can only do that with a True).
It's amazing how powerful macros are, and I'm really starting to like what I can do with them. I've already submitted these to hy.contrib, and I hope they get accepted. Try some Hy!
No comments:
Post a Comment