Posted at

dotoマクロ in CommonLisp

More than 1 year has passed since last update.

ClojureのdotoマクロをCommonLispに移植してみた。

実装

(defmacro doto (x &rest forms)

(let ((gx (gensym)))
`(let ((,gx ,x))
,@(mapcar (lambda (f)
(if (listp f)
`(,(first f) ,gx ,@(rest f))
`(,f ,gx)))
forms)
,gx)))

使ってみる

(defclass foo () ())

(defmethod baz ((this foo) x y)
(print (+ x y)))

(doto (make-instance 'foo) (baz 1 2) (baz 2 3))
; 3
; 5