Skip to content

Instantly share code, notes, and snippets.

@maio
Last active September 1, 2019 13:31
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 maio/59447181ffafed8191e3e2e202a9178f to your computer and use it in GitHub Desktop.
Save maio/59447181ffafed8191e3e2e202a9178f to your computer and use it in GitHub Desktop.
defnx
(defn foobar []
"foobar!")
(defn greet [name]
(print "Hello" (+ name "!")))
#!/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