Created
November 11, 2011 19:35
-
-
Save ijp/1358993 to your computer and use it in GitHub Desktop.
scheme version of clojure's ->
This file contains 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
;; 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