Last active
September 1, 2019 13:31
-
-
Save maio/59447181ffafed8191e3e2e202a9178f to your computer and use it in GitHub Desktop.
defnx
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
(defn foobar [] | |
"foobar!") | |
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
(defn greet [name] | |
(print "Hello" (+ name "!"))) | |
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
#!/usr/bin/env hy | |
(defmacro defnx [name args uri] | |
"Define function using code fetched from given URI" | |
(import [hy.models [HyExpression]]) | |
(defn update-fn-name [ast new-name] | |
(HyExpression (do (setv lst (list ast)) (assoc lst 1 new-name) lst))) | |
;; Fetch URI content during compilation so that it's cached and subsequent | |
;; program runs don't need to do so again. | |
(import [urllib.request [urlopen]]) | |
(setv ast (-> uri urlopen .read (.decode "utf-8") read-str (update-fn-name name))) | |
`(-> ~ast eval)) | |
(defnx greet [name] "https://gist.githubusercontent.com/maio/59447181ffafed8191e3e2e202a9178f/raw/e0057f6e1a9ecd31852f8ee09e9fae62a95d8e93/greet.hy") | |
(defnx xgreet [name] "https://gist.githubusercontent.com/maio/59447181ffafed8191e3e2e202a9178f/raw/e0057f6e1a9ecd31852f8ee09e9fae62a95d8e93/greet.hy") | |
(defnx foobar [] "https://gist.githubusercontent.com/maio/59447181ffafed8191e3e2e202a9178f/raw/fb84761dace9886be386e3490ec98a607a1d06a8/foobar.hy") | |
(greet "hy") | |
;; => Hello hy! | |
(xgreet "hy") | |
;; => Hello hy! | |
(print (foobar)) | |
;; => foobar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment