Skip to content

Instantly share code, notes, and snippets.

@ijp
Created November 11, 2011 19:35
Show Gist options
  • Save ijp/1358993 to your computer and use it in GitHub Desktop.
Save ijp/1358993 to your computer and use it in GitHub Desktop.
scheme version of clojure's ->
;; scheme version of clojure's ->
;; ->
;; macro
;; 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.
(define-syntax ->
(lambda (stx)
(define (combine x form)
(syntax-case form ()
[(proc args ...)
#`(proc #,x args ...)]
[id
(identifier? #'id)
#`(id #,x)]
[else
(syntax-violation '-> "Invalid form in ->" form)]))
(syntax-case stx ()
[(-> x) #'x]
[(-> x form forms ...)
(let ((form-list (syntax->list #'(form forms ...))))
(fold-left combine #'x form-list))])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment