Синтаксические сладости
Предлагаю просто поделиться удобными и красивыми синтаксическими трюками, может даже целыми подходами.
Вот, например, if с bind'ингом и it'ом:
Примеры использования:
(iif (< (:var xx (+ 12 x)) 34) (1+ xx) (-1 xx))
(iif some-variable (cons 'a it) '(a))
Вот, например, if с bind'ингом и it'ом:
(defun iif-lform-gather (lst)
(cond
((null lst) nil)
((listp (car lst))
(if (and (keywordp (caar lst)) (eq (caar lst) :var))
(prog1 (cons (cons (cadar lst) (cddar lst))
(iif-lform-gather (cdr lst)))
(setf (car lst) (caddar lst)))
(nconc (iif-lform-gather (car lst))
(iif-lform-gather (cdr lst)))))
(t (iif-lform-gather (cdr lst)))))
(defmacro iif (arg tform nform)
`(let ((it ,arg) ,@(iif-lform-gather arg))
(if it ,tform ,nform)))
(cond
((null lst) nil)
((listp (car lst))
(if (and (keywordp (caar lst)) (eq (caar lst) :var))
(prog1 (cons (cons (cadar lst) (cddar lst))
(iif-lform-gather (cdr lst)))
(setf (car lst) (caddar lst)))
(nconc (iif-lform-gather (car lst))
(iif-lform-gather (cdr lst)))))
(t (iif-lform-gather (cdr lst)))))
(defmacro iif (arg tform nform)
`(let ((it ,arg) ,@(iif-lform-gather arg))
(if it ,tform ,nform)))
Примеры использования:
(iif (< (:var xx (+ 12 x)) 34) (1+ xx) (-1 xx))
(iif some-variable (cons 'a it) '(a))