Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created November 4, 2016 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfsternberg/5ac39671b186da5a620f7ef452e9a342 to your computer and use it in GitHub Desktop.
Save elfsternberg/5ac39671b186da5a620f7ef452e9a342 to your computer and use it in GitHub Desktop.
; Macros for the '&' and '|' operators in Hy. The '&' and '|' symbols aren't very friendly to the Hy
; parser, and the operator version is a binary operator; extending it required a little macro magic,
; and it taught me a lot about Hy macros.
(import operator)
(defmacro/g! bwor (&rest args)
(let [[g!handler (fn [args]
(if (> (len args) 2)
`(operator.__or__ ~(car args) ~(g!handler (cdr args)))
`(operator.__or__ ~(car args) ~(car (cdr args)))))]]
(g!handler args)))
(defmacro/g! bwand (op &rest args)
(let [[g!handler (fn [args]
(if (> (len args) 2)
`(~op ~(car args) ~(g!handler (cdr args)))
`(~op ~(car args) ~(car (cdr args)))))]]
(g!handler args)))
(print (bwor 1 2 3 4 5))
(print (bwand 1 3 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment