Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active August 29, 2015 14:17
Show Gist options
  • Save m1el/084477b3db4c5f6bd202 to your computer and use it in GitHub Desktop.
Save m1el/084477b3db4c5f6bd202 to your computer and use it in GitHub Desktop.
; 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