j14i/cl-macros huggingface dataset:
Define a macro that eliminates this repetitive pattern:
(alist-bind (name fields) form (let ((sym (foreign-type-symbol name :cstruct *package*))) (let ((cstruct-fields (parse-fields fields))) (when (symbol-package sym) (pushnew `(:struct (,sym)) *foreign-record-list* :test #'equal)) `(define-foreign-record ',sym :struct ,(aval :bit-size form) ,(aval :bit-alignment form) ',cstruct-fields))))

Issue: Macro call that should be transformed by alist-bind

Macro definition:
(defmacro alist-bind ((&rest vars) alist &body body)
  "Inefficient but doesn't really matter here"
  (once-only (alist)
    `(let (,@(mapcar (lambda (x)
                       (if (consp x)
                           `(,(car x) (aval ,(cadr x) ,alist))
                           `(,x (aval ,(make-keyword x) ,alist))))
                     vars))
       ,@body)))

Macroexpansion:
(let ((#:alist241 form)) (let ((name (aval :name #:alist241)) (fields (aval :fields #:alist241))) (let ((sym (foreign-type-symbol name :cstruct *package*))) (let ((cstruct-fields (parse-fields fields))) (when (symbol-package sym) (pushnew (sb-int:quasiquote (:struct (#S(sb-impl::comma :expr sym :kind 0)))) *foreign-record-list* :test (function equal))) (sb-int:quasiquote (define-foreign-record (quote #S(sb-impl::comma :expr sym :kind 0)) :struct #S(sb-impl::comma :expr (aval :bit-size form) :kind 0) #S(sb-impl::comma :expr (aval :bit-alignment form) :kind 0) (quote #S(sb-impl::comma :expr cstruct-fields :kind 0))))))))
Create a Common Lisp macro for the following transformation. Before code:
(-> :g ((a (b))) (put a) (put b) (mt:open-latch latch))

What needs to change: Macro call that should be transformed by ->

Macro definition:
(defmacro -> (invariant &body args)
  "See flow:atomically"
  `(atomically ,invariant ,@args))

Macroexpansion:
(atomically :g ((a (b))) (put a) (put b) (cl-muth:open-latch latch))
Define a macro that eliminates this repetitive pattern:
(asif (car x) (setf x :oops) (setf it :yes!))

Issue: Macro call that should be transformed by asif

Macro definition:
(defmacro asif (test then &optional else)
  "Like IF, except binds the result of the test to IT (via LET) for
the the scope of the then-expression, and the test form to IT (via
SYMBOL-MACROLET) for the scope of the else-expression. Within scope of
the else-expression, IT can be set with SETF."
    `(let ((it ,test))
       (if it
	   ,then
	   (symbolic ignore-first ,test ,else))))

Macroexpansion:
(let ((it (car x))) (if it (setf x :oops) (symbolic ignore-first (car x) (setf it :yes!))))
Create a Common Lisp macro for the following transformation. Before code:
(define-simple-documentation-lookup variable cl:variable)

What needs to change: Macro call that should be transformed by define-simple-documentation-lookup

Macro definition:
(defmacro define-simple-documentation-lookup (class documentation-type)
  `(defmethod documentation ((,class ,class))
     ,(if (eql documentation-type T)
          `(cl:documentation (object ,class) 'T)
          `(cl:documentation (designator ,class) ',documentation-type))))

Macroexpansion:
(defmethod documentation ((variable variable)) (common-lisp:documentation (designator variable) (quote common-lisp:variable)))
Create a Common Lisp macro for the following transformation. Before code:
(define-test dependency (false (sleep 1)))

What needs to change: Macro call that should be transformed by define-test

Macro definition:
(defmacro define-test (name &body arguments-and-body)
  (destructuring-bind (nparent name) (if (listp name) name (list NIL name))
    (form-fiddle:with-body-options (body options parent home (test-class 'test) (compile-at :compile-time) defun) arguments-and-body
      (let ((body (remove 'define-test body :key (lambda (a) (when (listp a) (car a))) :test #'eql))
            (defs (remove 'define-test body :key (lambda (a) (when (listp a) (car a))) :test-not #'eql))
            (home-form (or home `(find-package ,(package-name *package*)))))
        (when (and parent nparent)
          (error "Cannot specify parent through name and through a keyword argument at the same time!"))
        `(let ((*package* (find-package ,(package-name *package*)))) ; Make sure package stays consistent throughout initialisation.
           (setf (find-test ',name ,home-form)
                 (ensure-test ',test-class
                              :name ',name
                              :home ,home-form
                              :tests (list ,@(loop for form in body
                                                   collect (ecase compile-at
                                                             (:compile-time `(lambda () ,form))
                                                             (:execute `(lambda () (call-compile ',form))))))
                              :parent ',(or parent nparent)
                              ,@(loop for option in options
                                      collect `',option)))
           ,@(loop for (def subname . body) in defs
                   collect `(,def (,name ,subname)
                              :home ,home
                              ,@body))
           ,@(when defun
               `((defun ,(if (stringp name) (read-symbol name) name) (&rest test-args)
                   (apply #'test (find-test ',name ,home-form) test-args))))
           ',name)))))

Macroexpansion:
(let ((*package* (find-package #A((9) base-char . "PARACHUTE")))) (setf (find-test (quote dependency) (find-package #A((9) base-char . "PARACHUTE"))) (ensure-test (quote test) :name (quote dependency) :home (find-package #A((9) base-char . "PARACHUTE")) :tests (list (lambda nil (false (sleep 1)))) :parent (quote nil))) (quote dependency))
You are a Common Lisp macro expert. Given this code pattern:
(bind (((:labels my-oddp (x)) (cond ((<= x 0) nil) ((= x 1) t) (t (my-oddp (- x 2)))))) (ensure (my-oddp 1)) (ensure (my-oddp 7)) (ensure-null (my-oddp 2)))

Write a macro that addresses: Macro call that should be transformed by bind

Macro definition:
(defmacro bind ((&rest bindings) &body body)
  "Bind is a replacement for let*, destructuring-bind, multiple-value-bind and more. 

An example is probably the best way to describe its syntax:

    \(bind \(\(a 2\)
           \(\(b &rest args &key \(c 2\) &allow-other-keys\) '\(:a :c 5 :d 10 :e 54\)\)
           \(\(:values d e\) \(truncate 4.5\)\)
           \(\(:structure xxx- slot1 slot2\) \(make-xxx\)\)
           \(\(:flet name \(arg1 arg2\)\) \(+ arg1 arg2\)\)\)
         \(list a b c d e args\)\)

Simple bindings are as in let*. Destructuring is done if the first item
in a binding is a list. Multiple value binding is done if the first item
in a binding is a list and the first item in the list is ':values'. Other
forms have their own syntax. For example, :structure first has the conc
name and then slot names whereas :flet has the function name and a list
of arguments and then the function body (in an implicit progn)."
  (let (declarations)
    (loop while (and (consp (car body)) (eq (caar body) 'declare)) do
          (push (first body) declarations)
          (setf body (rest body)))
    (if bindings
	(let ((*all-declarations* (bind-expand-declarations (nreverse declarations))))
	  (prog1
	      (bind-macro-helper bindings body)
	    (check-for-unused-variable-declarations *all-declarations*)))
        `(locally
             ,@declarations
           ,@body))))

Macroexpansion:
(labels ((my-oddp (x) (progn (cond ((<= x 0) nil) ((= x 1) t) (t (my-oddp (- x 2))))))) (progn (ensure (my-oddp 1)) (ensure (my-oddp 7)) (ensure-null (my-oddp 2))))
Write a Common Lisp macro to handle this code pattern:
;; Closure variables are opaque - can't inspect or modify them
(let ((acc 0) (mul 1))
  (lambda (n)
    (incf acc n)
    (setf mul (* mul n))))

;; To access acc/mul: must add specific getter/setter methods
;; This doesn't scale - every variable needs its own accessor

Problem: Closure internals are opaque — want a general protocol for reading/writing any closed-over variable

Macro definition:
(defun pandoriclet-get (letargs)
  `(case sym
     ,@(mapcar #`((,(car a1)) ,(car a1))
               letargs)
     (t (error
          "Unknown pandoric get: ~a"
          sym))))

(defun pandoriclet-set (letargs)
  `(case sym
     ,@(mapcar #`((,(car a1))
                   (setq ,(car a1) val))
               letargs)
     (t (error
          "Unknown pandoric set: ~a"
          sym val))))

(defmacro pandoriclet (letargs &rest body)
  (let ((letargs (cons
                   '(this)
                   (let-binding-transform
                     letargs))))
    `(let (,@letargs)
       (setq this ,@(last body))
       ,@(butlast body)
       (dlambda
         (:pandoric-get (sym)
           ,(pandoriclet-get letargs))
         (:pandoric-set (sym val)
           ,(pandoriclet-set letargs))
         (t (&rest args)
           (apply this args))))))

Macroexpansion:
;; (pandoriclet ((acc 0) (mul 1))
;;   (lambda (n) (incf acc n) (setf mul (* mul n))))
;;
;; Now:
;; (closure :pandoric-get 'acc)    => value of acc
;; (closure :pandoric-set 'acc 100) => sets acc to 100
;; (closure 5)                      => normal call
;;
;; with-pandoric makes them look like regular variables:
;; (with-pandoric (acc mul) closure
;;   (setf acc (+ acc 10)))
You are a Common Lisp macro expert. Given this code pattern:
(defmethod nil
  (progn))

Write a macro that addresses: Macro call that should be transformed by DEFMETHOD

Macro definition:
(defmacro DEFMETHOD ...)

Macroexpansion:
(common-lisp:defmethod nil (progn))
Write a Common Lisp macro to handle this code pattern:
(whichever (1+ x))

Problem: Macro call that should be transformed by whichever

Macro definition:
(defmacro whichever (&rest possibilities &environment env)
  "Evaluates exactly one of POSSIBILITIES, chosen at random."
  (setf possibilities (mapcar (lambda (p) (macroexpand p env)) possibilities))
  (let ((length (length possibilities)))
    (cond
      ((= 1 length)
       (first possibilities))
      ((every #'constantp possibilities)
       `(svref (load-time-value (vector ,@possibilities)) 
               (random ,length)))
      (T
       (labels ((expand (possibilities position random-number)
                  (if (null (cdr possibilities))
                      (car possibilities)
                      (let* ((length (length possibilities))
                             (half (truncate length 2))
                             (second-half (nthcdr half possibilities))
                             (first-half (butlast possibilities (- length half))))
                        `(if (< ,random-number ,(+ position half))
                             ,(expand first-half position random-number)
                             ,(expand second-half (+ position half) random-number))))))
         (with-gensyms (random-number)
           `(let ((,random-number (random ,length)))
              ,(expand possibilities 0 random-number))))))))

Macroexpansion:
(1+ x)
You are a Common Lisp macro expert. Given this code pattern:
(with-special-initargs* nil)

Write a macro that addresses: Macro call that should be transformed by WITH-SPECIAL-INITARGS*

Macro definition:
(defmacro WITH-SPECIAL-INITARGS* ...)

Macroexpansion:
(progn)
Define a macro that eliminates this repetitive pattern:
(with-special-initargs* nil
  :key
  nil)

Issue: Macro call that should be transformed by WITH-SPECIAL-INITARGS*

Macro definition:
(defmacro WITH-SPECIAL-INITARGS* ...)

Macroexpansion:
(progn :key nil)
Define a macro that eliminates this repetitive pattern:
(def-dynenv-var nil nil)

Issue: Macro call that should be transformed by DEF-DYNENV-VAR

Macro definition:
(defmacro DEF-DYNENV-VAR ...)

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defvar #:nil172 (load-time-value nil))
  (setf (get 'nil 'dynenv-var) '#:nil172)
  (define-symbol-macro nil (get-dynenv-var 'nil)))
Write a Common Lisp macro to handle this code pattern:
(with-table node hash depth (bitmap array bits index hit) (flet ((%insert (table) (%set-insert table key hash (1+ depth) test))) (let ((new-node (cond (hit (%insert (aref array index))) ((= depth 6) (make-instance 'set-leaf :key key)) (t (%insert (make-instance 'set-table)))))) (make-instance 'set-table :bitmap (logior bitmap (ash 1 bits)) :table (funcall (if hit #'vec-update #'vec-insert) array index new-node)))))

Problem: Macro call that should be transformed by with-table

Macro definition:
(defmacro with-table (node hash depth
                      (bitmap array bits index hit)
                      &body body)
  "Bitshifting in HAMT tables"
  `(let* ((,bitmap (table-bitmap ,node))
          (,array (table-array ,node))
          (,bits (get-bits ,hash ,depth))
          (,index (get-index ,bits ,bitmap))
          (,hit (logbitp ,bits ,bitmap)))
     ,@body))

Macroexpansion:
(let* ((bitmap (table-bitmap node)) (array (table-array node)) (bits (get-bits hash depth)) (index (get-index bits bitmap)) (hit (logbitp bits bitmap))) (flet ((%insert (table) (%set-insert table key hash (1+ depth) test))) (let ((new-node (cond (hit (%insert (aref array index))) ((= depth 6) (make-instance (quote set-leaf) :key key)) (t (%insert (make-instance (quote set-table))))))) (make-instance (quote set-table) :bitmap (logior bitmap (ash 1 bits)) :table (funcall (if hit (function vec-update) (function vec-insert)) array index new-node)))))
Generate a defmacro form that transforms code like this:
(dletf* 'some-name)

Pattern to recognize: Macro call that should be transformed by DLETF*

Macro definition:
(defmacro DLETF* ...)

Macroexpansion:
(dlet ('nil)
  (dlet ((some-name nil))
    (progn)))
Define a macro that eliminates this repetitive pattern:
(bind (((:labels constant (x)) (declare (ignore x)) 42)) (ensure-same (constant 1) 42))

Issue: Macro call that should be transformed by bind

Macro definition:
(defmacro bind ((&rest bindings) &body body)
  "Bind is a replacement for let*, destructuring-bind, multiple-value-bind and more. 

An example is probably the best way to describe its syntax:

    \(bind \(\(a 2\)
           \(\(b &rest args &key \(c 2\) &allow-other-keys\) '\(:a :c 5 :d 10 :e 54\)\)
           \(\(:values d e\) \(truncate 4.5\)\)
           \(\(:structure xxx- slot1 slot2\) \(make-xxx\)\)
           \(\(:flet name \(arg1 arg2\)\) \(+ arg1 arg2\)\)\)
         \(list a b c d e args\)\)

Simple bindings are as in let*. Destructuring is done if the first item
in a binding is a list. Multiple value binding is done if the first item
in a binding is a list and the first item in the list is ':values'. Other
forms have their own syntax. For example, :structure first has the conc
name and then slot names whereas :flet has the function name and a list
of arguments and then the function body (in an implicit progn)."
  (let (declarations)
    (loop while (and (consp (car body)) (eq (caar body) 'declare)) do
          (push (first body) declarations)
          (setf body (rest body)))
    (if bindings
	(let ((*all-declarations* (bind-expand-declarations (nreverse declarations))))
	  (prog1
	      (bind-macro-helper bindings body)
	    (check-for-unused-variable-declarations *all-declarations*)))
        `(locally
             ,@declarations
           ,@body))))

Macroexpansion:
(labels ((constant (x) (declare (ignore x)) (progn 42))) (progn (ensure-same (constant 1) 42)))
Create a Common Lisp macro for the following transformation. Before code:
(fgathering nil)

What needs to change: Macro call that should be transformed by FGATHERING

Macro definition:
(defmacro FGATHERING ...)

Macroexpansion:
(flet ()
  (values))
Generate a defmacro form that transforms code like this:
(with-wrap-attempt () name (let ((extern (make-instance 'foreign-extern :name name :c-symbol c-symbol :type (ensure-type type "extern ~S of type ~S" name type)))) (setf (gethash name *foreign-externs*) extern)))

Pattern to recognize: Macro call that should be transformed by with-wrap-attempt

Macro definition:
(defmacro with-wrap-attempt ((&optional format-control &rest format-args) wrappable-name &body body)
  `(call-with-wrap-attempt ,wrappable-name (lambda () ,@body) ,format-control (list ,@format-args)))

Macroexpansion:
(call-with-wrap-attempt name (lambda nil (let ((extern (make-instance (quote foreign-extern) :name name :c-symbol c-symbol :type (ensure-type type "extern ~S of type ~S" name type)))) (setf (gethash name *foreign-externs*) extern))) nil (list))
Create a Common Lisp macro for the following transformation. Before code:
(defmethod* nil
    :key
  nil)

What needs to change: Macro call that should be transformed by DEFMETHOD*

Macro definition:
(defmacro DEFMETHOD* ...)

Macroexpansion:
(defmethod nil :key (with-yield ()))
Write a Common Lisp macro to handle this code pattern:
(def-unbound-dynenv-fun nil)

Problem: Macro call that should be transformed by DEF-UNBOUND-DYNENV-FUN

Macro definition:
(defmacro DEF-UNBOUND-DYNENV-FUN ...)

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun nil (&rest #:args) (apply (dynenv-function nil) #:args))
  (defvar #:nil142)
  (setf (get 'nil 'dynenv-fun) '#:nil142))
Define a macro that eliminates this repetitive pattern:
(with-yield-binds (let ((when-else (loop for obj in (conjunctive-op-expressions op) collect (with-table-name nil (yield obj))))) (format nil "CASE ~{~A~^ ~} END" when-else)))

Issue: Macro call that should be transformed by with-yield-binds

Macro definition:
(defmacro with-yield-binds (&body body)
  `(let ((*bind-values* nil)
         (*use-global-bind-values* t))
     (values
      (progn ,@body)
      (loop for bind in (reverse *bind-values*)
            append bind))))

Macroexpansion:
(let ((*bind-values* nil) (*use-global-bind-values* t)) (values (progn (let ((when-else (loop for obj in (conjunctive-op-expressions op) collect (with-table-name nil (yield obj))))) (format nil "CASE ~{~A~^ ~} END" when-else))) (loop for bind in (reverse *bind-values*) append bind)))
Write a Common Lisp macro to handle this code pattern:
(when (find (subseq arg 2) *clon-shuffle-opts-long* :test #'string-equal) (setf expecting-arg t))

Problem: Macro call that should be transformed by when

Macro definition:
(defmacro when (test &body forms)
  `(if ,test
	   ,(cl:if (cadr forms)
			   `(progn ,@forms)
			   (car forms))))

Macroexpansion:
(if (find (subseq arg 2) *clon-shuffle-opts-long* :test (function string-equal)) (setf expecting-arg t))
Generate a defmacro form that transforms code like this:
(make-name-transformer nil (progn))

Pattern to recognize: Macro call that should be transformed by MAKE-NAME-TRANSFORMER

Macro definition:
(defmacro MAKE-NAME-TRANSFORMER ...)

Macroexpansion:
(lambda (name definition)
  (declare (ignorable definition))
  (concatenate-symbol nil (progn)))
You are a Common Lisp macro expert. Given this code pattern:
(define-custom-var (:object-key *object-key-handler*))

Write a macro that addresses: Macro call that should be transformed by define-custom-var

Macro definition:
(defmacro define-custom-var ((key name) &rest other-args)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (progn (pushnew '(,name . ,key) *custom-vars* :test #'equal)
            (defvar ,name ,@other-args))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (progn (pushnew (quote (*object-key-handler* . :object-key)) *custom-vars* :test (function equal)) (defvar *object-key-handler*)))
You are a Common Lisp macro expert. Given this code pattern:
(builder :session :csrf #'(lambda (env) (let ((req (make-request env))) `(200 (:content-type "text/html") (,(if (and (eq :post (request-method req)) (assoc "name" (request-body-parameters req) :test #'string=)) (cdr (assoc "name" (request-body-parameters req) :test #'string=)) (html-form env)))))))

Write a macro that addresses: Macro call that should be transformed by builder

Macro definition:
(defmacro builder (&rest app-or-middlewares)
  (let ((middlewares (butlast app-or-middlewares)))
    `(reduce #'funcall
             (remove-if
              #'null
              (list
               ,@(loop for mw in middlewares
                       when mw
                         collect (convert-to-middleware-form mw))))
             :initial-value (to-app ,(car (last app-or-middlewares)))
             :from-end t)))

Macroexpansion:
(reduce (function funcall) (remove-if (function null) (list (find-middleware :session) (find-middleware :csrf))) :initial-value (to-app (function (lambda (env) (let ((req (make-request env))) (sb-int:quasiquote (200 (:content-type "text/html") (#S(sb-impl::comma :expr (if (and (eq :post (request-method req)) (assoc "name" (request-body-parameters req) :test (function string=))) (cdr (assoc "name" (request-body-parameters req) :test (function string=))) (html-form env)) :kind 0)))))))) :from-end t)
Define a macro that eliminates this repetitive pattern:
(define-mustache-character #\# (make-instance 'section-start-tag :text arg-text :end end :open-delimiter *open-delimiter* :close-delimiter *close-delimiter*))

Issue: Macro call that should be transformed by define

Macro definition:
(defmacro define (name template)
  "Define a named renderer of string TEMPLATE."
  `(setf (symbol-function ',name)
         (compile-template ,template)))

Macroexpansion:
(set-mustache-character #\NUMBER_SIGN (lambda (raw-text arg-text escapep start end) (declare (ignorable raw-text arg-text escapep start end) (type simple-string raw-text arg-text) (boolean escapep) (offset start end)) (make-instance (quote section-start-tag) :text arg-text :end end :open-delimiter *open-delimiter* :close-delimiter *close-delimiter*)))
Define a macro that eliminates this repetitive pattern:
(with-decoder-simple-list-semantics (setf x (decode-json-from-string "{\"x\":1}")) (is (equal (symbol-package (caar x)) (find-package :keyword))))

Issue: Macro call that should be transformed by with-decoder-simple-list-semantics

Macro definition:
(defmacro with-decoder-simple-list-semantics (&body body)
  "Execute BODY in a dynamic environement where the decoder semantics
is such as set by SET-DECODER-SIMPLE-LIST-SEMANTICS."
  `(with-shadowed-custom-vars
     (set-decoder-simple-list-semantics)
     ,@body))

Macroexpansion:
(with-shadowed-custom-vars (set-decoder-simple-list-semantics) (setf x (decode-json-from-string "{\"x\":1}")) (is (equal (symbol-package (caar x)) (find-package :keyword))))