Created
November 4, 2016 05:13
-
-
Save elfsternberg/5ac39671b186da5a620f7ef452e9a342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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