Skip to content

Instantly share code, notes, and snippets.

@addisaden
Last active November 10, 2022 17:05
Show Gist options
  • Save addisaden/20d6fff43259a1b4dc871d5dbdda4141 to your computer and use it in GitHub Desktop.
Save addisaden/20d6fff43259a1b4dc871d5dbdda4141 to your computer and use it in GitHub Desktop.
(import flask)
(setv app (flask.Flask "Hello World"))
; flask would start like this
;
; app = flask.Flask("Hello World")
;
; @app.route("/noprefix")
; def noprefix_route():
; return "no prefix"
;
; without prefix
(defn say-noprefix [] "no prefix")
((app.route "/noprefix") say-noprefix)
; To rewrite the normal order like above
; we define a macro
(defmacro with-prefix [code-a code-b]
(quasiquote
(do
(unquote code-b)
(unquote code-a))))
; now we can do it like this
(with-prefix
((app.route "/") say-hi)
(defn say-hi [] "hi"))
; The second macro changes also the function name
(defmacro with-prefix-2 [name decorator arglist funblock]
`(do
(defn ~name ~arglist ~funblock)
(~decorator ~name)))
; so we can do this
(with-prefix-2
test-route (app.route "/test")
[] (flask.jsonify "hello"))
(with-prefix-2
check-route (app.route "/check/<checkid>")
[checkid] (+ "The id is " checkid))
(app.run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment