Last active
August 29, 2015 14:17
-
-
Save m1el/084477b3db4c5f6bd202 to your computer and use it in GitHub Desktop.
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
; imperative style | |
(define-syntax (-> x) | |
(let* ((sym (gensym)) | |
(arg (cdr (syntax-e x))) | |
(lines (map (λ (f) | |
(if (identifier? f) | |
#`(set! #,sym (#,f #,sym)) | |
#`(set! #,sym (#,@f #,sym)))) | |
(cdr arg)))) | |
#`(let ((#,sym #,(car arg))) | |
#,@lines | |
#,sym))) | |
; full macro expansion | |
(define-syntax (-> x) | |
(let ((arg (cdr (syntax-e x)))) | |
(foldl (λ (l r) | |
(if (identifier? l) | |
#`(#,l #,r) | |
#`(#,@l #,r))) | |
(car arg) (cdr arg)))) | |
; functional style | |
(define-syntax (-> x) | |
(let* ((arg (cdr (syntax-e x))) | |
(v (car arg)) | |
(fns (map (λ (f) (if (identifier? f) f | |
`(curry ,@f))) | |
(reverse (cdr arg))))) | |
#`((compose #,@fns) #,v))) | |
; example: | |
; > (-> 2 sqrt (+ 1)) | |
; 2.414213562373095 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment