| Paste number 52651: | Functional Reader Macros |
| Pasted by: | lispcast |
| When: | 1 year, 6 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+14MJ |
| Channel: | None |
| Paste contents: |
;; borrowed from
;; <http://rottcodd.wordpress.com/2007/11/29/lambda-shortcut/>
(set-dispatch-macro-character
#\# #\L #'(lambda (stream sub-character infix-parameter)
(when infix-parameter
(error "#~a does not take an integer infix parameter."
sub-character))
`#'(lambda (,(intern "_"))
,(read stream t nil t))))
;; Compose macro
;; Usage: #M(abs /) ==> (lambda (a b) (abs (/ a b)))
(set-dispatch-macro-character
#\# #\M #'(lambda (stream sub-character infix-parameter)
(when infix-parameter
(error "#~a does not take an integer infix parameter."
sub-character))
`(alexandria:compose
,@(loop for x in (read stream t nil t)
collect (typecase x
(function x)
(cons x)
(t `#',x))))))
;; Curry macro
;; Usage: #R(* 3) ==> (lambda (&rest r) (apply #'* 3 r))
(set-dispatch-macro-character
#\# #\R #'(lambda (stream sub-character infix-parameter)
(when infix-parameter
(error "#~a does not take an integer infix parameter."
sub-character))
(let ((expr (read stream t nil t)))
`(alexandria:curry
,(typecase (first expr)
(function (first expr))
(cons (first expr))
(t `#',(first expr)))
,@(rest expr)))))
This paste has no annotations.