** drewc JOIN #emacs
<ijp> nicferrier: cut is a macro so that you can do e.g. (cut
            + foo bar <> baz <>) instead of (lambda (x y) (+ foo
ORG-LIST-END-MARKER
            bar x baz y))
15:33 <ijp> rudybot: (require srfi/26)
15:33 <rudybot> ijp: Done.
15:33 <ijp> rudybot: ((cut list 'foo <> 'baz) 'bar)
15:33 <rudybot> ijp: ; Value:  (foo bar baz)
(defun map-cut (fn &rest args &aux arg-list)
  (let* ((body (mapcar (lambda (arg) (case arg
                                       (<> (let ((sym (gensym)))
                                             (push sym arg-list)
                                             sym))
                                       (otherwise arg))) 
                       args))
         (fn-form (nconc (etypecase fn 
                           (symbol (list fn))
                           (function `(funcall ,fn)))
                         body)))
    `(lambda ,(nreverse arg-list) ,fn-form)))

(defmacro cut (function-name &rest args-or-<>)
  `(apply #'map-cut ',function-name (quote ,args-or-<>)))


;; CL-USER> (#.(cut list 1 2 <> 4 <>) 3 "t")
;; (1 2 3 4 "t")
;; CL-USER> (apply #'map-cut '(list 1 2 <> 4 <>))
;; (LAMBDA (#:G925 #:G926) (LIST 1 2 #:G925 4 #:G926))
blog comments powered by Disqus