I recently entertained myself by porting two Clojure macros to Racket. I present them here hoping they will be useful to somebody more.
Note: The Racket implementations can be made slightly less clunky with a convenience macro like define-syntax-case; stx is not used by me.
->Usage:(-> x) (-> x form) (-> x form & more)
Threads the expr through the forms. Inserts x as the second item in the first form, making a list of it if it is not a list already. If there are more forms, inserts the first form as the second item in second form, etc.
(defmacro ->
([x] x)
([x form] (if (seq? form)
(with-meta `(~(first form) ~x ~@(next form)) (meta form))
(list form x)))
([x form & more] `(-> (-> ~x ~form) ~@more)))
(require (for-syntax syntax/stx))
(define-syntax (-> stx)
(syntax-case stx ()
((_ x) #'x)
((_ x form) (if (stx-list? #'form)
#`(#,(stx-car #'form) x #,@(stx-cdr #'form))
#'(form x)))
((_ x form . more) #'(-> (-> x form) . more))))
->>Usage:(->> x form) (->> x form & more)
Threads the expr through the forms. Inserts x as the last item in the first form, making a list of it if it is not a list already. If there are more forms, inserts the first form as the last item in second form, etc.
(defmacro ->>
([x form] (if (seq? form)
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x)))
([x form & more] `(->> (->> ~x ~form) ~@more)))
(require (for-syntax syntax/stx))
(define-syntax (->> stx)
(syntax-case stx ()
((_ x form) (if (stx-list? #'form)
#`(#,(stx-car #'form) #,@(stx-cdr #'form) x)
#'(form x)))
((_ x form . more) #'(->> (->> x form) . more))))