j14i/cl-macros huggingface dataset:
Generate a defmacro form that transforms code like this:
(do-generator-value-list (vars (apply #'multi-gen (loop for g in gens collect (list g :fill-value fillspec)))) (apply #'yield (mapcar #'car vars)))

Pattern to recognize: Macro call that should be transformed by do-generator

Macro definition:
(defmacro do-generator ((var &rest vars-and-generator) &body body)
  "Steps through the specified generator, binding the value it returns to var, then executing the body for each step. If multiple variables are specified and the generator returns multiple values, the extra values will be bound to the extra variables."
  (let ((g (gensym)))
    `(let ((,g ,(car (last vars-and-generator))))
       (loop 
	  do (if-generator ((,var ,@(butlast vars-and-generator)) ,g)
			   (progn
			     ,@body)
			   (return))))))

Macroexpansion:
(let ((#:g158 (apply (function multi-gen) (loop for g in gens collect (list g :fill-value fillspec))))) (loop do (if-generator (vars #:g158) (progn (apply (function yield) (mapcar (function car) vars))) (return))))
Generate a defmacro form that transforms code like this:
(signal-syntax-error "Variable length look-behind not implemented \(yet): ~S." parse-tree)

Pattern to recognize: Macro call that should be transformed by signal-syntax-error

Macro definition:
(defmacro signal-syntax-error (format-control &rest format-arguments)
  `(signal-syntax-error* nil ,format-control ,@format-arguments))

Macroexpansion:
(signal-syntax-error* nil "Variable length look-behind not implemented (yet): ~S." parse-tree)
You are a Common Lisp macro expert. Given this code pattern:
(defun/1 freopen (filename mode fd) (handler-case (progn (fclose fd) (clearerr fd) (setf (fd-stream fd) (open-stream filename mode))) (error () (setf errno EIO) NULL)))

Write a macro that addresses: Macro call that should be transformed by defun/1

Macro definition:
(defmacro defun/1 (name arglist &body body)
  "Lisp-1 defun; makes function pointers work."
  `(progn
     (defun ,name ,arglist
       (declare (optimize (speed 0) (debug 3)
                          ;; FIXME: Why do minor warnings result in load errors?
                          #+sbcl (sb-ext:inhibit-warnings 3)))
        
       ,@body)
     (defparameter ,name (vacietis.c:mkptr& (symbol-function ',name)))))

Macroexpansion:
(progn (defun freopen (filename mode fd) (declare (optimize (speed 0) (debug 3) (sb-ext:inhibit-warnings 3))) (handler-case (progn (fclose fd) (clearerr fd) (setf (fd-stream fd) (open-stream filename mode))) (error nil (setf errno eio) null))) (defparameter freopen (vacietis.c:mkptr& (symbol-function (quote freopen)))))
Define a macro that eliminates this repetitive pattern:
(->* nil)

Issue: Macro call that should be transformed by ->*

Macro definition:
(defmacro ->* ...)

Macroexpansion:
(-> nil)
Generate a defmacro form that transforms code like this:
(defgenerator imap (function &rest generators) "The generator equivalent of the mapcar function. Applies function to the values of the supplied generators, emitting the result. Eg: (imap #'* (list->generator '(2 3 4)) (list->generator '(4 5 6))) -> 8 15 24" (do-generator-value-list (vals (apply #'multi-gen generators)) (yield (apply function (mapcar #'car vals)))))

Pattern to recognize: Macro call that should be transformed by defgenerator

Macro definition:
(defmacro defgenerator (name arguments &body body)
  (multiple-value-bind (remaining-forms declarations doc-string)
      (alexandria:parse-body body :documentation t)
    `(defun ,name ,arguments
       ,doc-string
       ,@declarations
       (with-yield
	 ,@remaining-forms))))

Macroexpansion:
(defun imap (function &rest generators) "The generator equivalent of the mapcar function. Applies function to the values of the supplied generators, emitting the result. Eg: (imap #'* (list->generator '(2 3 4)) (list->generator '(4 5 6))) -> 8 15 24" (with-yield (do-generator-value-list (vals (apply (function multi-gen) generators)) (yield (apply function (mapcar (function car) vals))))))
Write a Common Lisp macro to handle this code pattern:
(count-of 'some-name)

Problem: Macro call that should be transformed by COUNT-OF

Macro definition:
(defmacro COUNT-OF ...)

Macroexpansion:
(lc ('some-name) count)
Write a Common Lisp macro to handle this code pattern:
(dletf* nil
  nil)

Problem: Macro call that should be transformed by DLETF*

Macro definition:
(defmacro DLETF* ...)

Macroexpansion:
(progn nil)
You are a Common Lisp macro expert. Given this code pattern:
(lambda-ematch (PATTERN BODY...))

Write a macro that addresses: Macro call that should be transformed by lambda-ematch

Macro definition:
(defmacro lambda-ematch (&body clauses)
  "Equivalent to (lambda (arg) (ematch arg CLAUSES...))."
  (with-unique-names (arg)
    `(lambda (,arg) (ematch ,arg ,@clauses))))

Macroexpansion:
(lambda (#:arg123) (ematch #:arg123 (pattern body...)))
Generate a defmacro form that transforms code like this:
(*let 'some-name)

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

Macro definition:
(defmacro *LET ...)

Macroexpansion:
(let* ((quote nil) (some-name nil)))
You are a Common Lisp macro expert. Given this code pattern:
(defpackage nil)

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

Macro definition:
(defmacro DEFPACKAGE ...)

Macroexpansion:
(prog1 (common-lisp:defpackage nil))
You are a Common Lisp macro expert. Given this code pattern:
(with-local-readtable
  'some-name)

Write a macro that addresses: Macro call that should be transformed by WITH-LOCAL-READTABLE

Macro definition:
(defmacro WITH-LOCAL-READTABLE ...)

Macroexpansion:
(bind ((*readtable* (copy-readtable *readtable*)))
  'some-name)
Write a Common Lisp macro to handle this code pattern:
(define-method-combination% min :operator min :identity-with-one-argument t)

Problem: Macro call that should be transformed by define-method-combination%

Macro definition:
(defmacro define-method-combination% (name &rest args)
  "Define a method-combination for the static-dispatch system only.

   Arguments are of the same form as to CL:DEFINE-METHOD-COMBINATION."

  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (setf
      (combination-function ',name)
      ,(match args
         ((or nil
              (list* (and (type symbol) (not nil)) _))

          (destructuring-bind (&key (operator name) (identity-with-one-argument nil))
              args

            `(method-combination-function-short ,name ,operator ,identity-with-one-argument)))

         (_
          `(method-combination-function ,name ,@args))))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (setf (combination-function (quote min)) (method-combination-function-short min min t)))
Define a macro that eliminates this repetitive pattern:
(builder nil (if t nil nil) nil *app*)

Issue: 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 (let ((#:res171 (if t nil nil))) (typecase #:res171 (keyword (find-middleware #:res171)) (cons (if (keywordp (car #:res171)) (let ((#:res-mw170 (find-middleware (car #:res171)))) (lambda (#:app169) (apply #:res-mw170 #:app169 (cdr #:res171)))) #:res171)) (standard-object (lambda (#:app169) (funcall (intern (string :wrap) :clack.middleware) #:res171 #:app169))) (otherwise #:res171))))) :initial-value (to-app *app*) :from-end t)
Generate a defmacro form that transforms code like this:
(->*)

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

Macro definition:
(defmacro ->* ...)

Macroexpansion:
(->)
Define a macro that eliminates this repetitive pattern:
(fmap (lambda (s) (let ((*read-default-float-format* 'double-float)) (cl:float (read-from-string s) 1.0d0))) (funcall (recognize (*> #'integer (opt (*> (char #\.) (take-while1 #'digit?))))) offset))

Issue: Macro call that should be transformed by fmap

Macro definition:
(defmacro fmap (f thing)
  "Apply a pure function to the inner contents of some parsing operation, if the
parsing itself was successful."
  `(multiple-value-bind (res next) ,thing
     (if (failure? res)
         (fail next)
         (values (funcall ,f res) next))))

Macroexpansion:
(multiple-value-bind (res next) (funcall (recognize (*> (function integer) (opt (*> (char #\FULL_STOP) (take-while1 (function digit?)))))) offset) (if (failure? res) (fail next) (values (funcall (lambda (s) (let ((*read-default-float-format* (quote double-float))) (common-lisp:float (read-from-string s) 1.0d0))) res) next)))
You are a Common Lisp macro expert. Given this code pattern:
(define-definition-resolver method (designator package) (when (designator-generic-function-p designator) (loop for method in (sb-mop:generic-function-methods (fdefinition designator)) collect (make-instance 'method :designator designator :package package :method method))))

Write a macro that addresses: Macro call that should be transformed by define-definition-resolver

Macro definition:
(defmacro define-definition-resolver (name args &body body)
  `(progn (setf (definition-resolver ',name)
                (lambda ,args ,@body))
          ',name))

Macroexpansion:
(progn (setf (definition-resolver (quote method)) (lambda (designator package) (when (designator-generic-function-p designator) (loop for method in (sb-mop:generic-function-methods (fdefinition designator)) collect (make-instance (quote method) :designator designator :package package :method method))))) (quote method))
You are a Common Lisp macro expert. Given this code pattern:
(define-simple-documentation-lookup macro cl:function)

Write a macro that addresses: 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 ((macro macro)) (common-lisp:documentation (designator macro) (quote common-lisp:function)))
Write a Common Lisp macro to handle this code pattern:
(defmethod* nil
    nil)

Problem: Macro call that should be transformed by DEFMETHOD*

Macro definition:
(defmacro DEFMETHOD* ...)

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

Problem: Macro call that should be transformed by WITH-DYNENV

Macro definition:
(defmacro WITH-DYNENV ...)

Macroexpansion:
(multiple-value-bind (#:var-expansion122 #:var-expanded-p123)
    (macroexpand-1 'var-space nil)
  (multiple-value-bind (#:fun-expansion124 #:fun-expanded-p125)
      (macroexpand-1 'fun-space nil)
    (let ((*var-space* (when #:var-expanded-p123 #:var-expansion122))
          (*fun-space* (when #:fun-expanded-p125 #:fun-expansion124))
          (*within-captured-dynenv* t)))))
Write a Common Lisp macro to handle this code pattern:
(bind (((:flet doit (x)) "if I knew how to get the docstring out of flet, I'd test it." (setf x (* 2 x)) (setf x (+ x 3)) x)) (ensure-same (doit 1) 5) (ensure-same (doit 2) 7))

Problem: 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:
(flet ((doit (x) "if I knew how to get the docstring out of flet, I'd test it." (progn (setf x (* 2 x)) (setf x (+ x 3)) x))) (progn (ensure-same (doit 1) 5) (ensure-same (doit 2) 7)))
Write a Common Lisp macro to handle this code pattern:
(as->* nil
  (progn))

Problem: Macro call that should be transformed by AS->*

Macro definition:
(defmacro AS->* ...)

Macroexpansion:
(progn)
Generate a defmacro form that transforms code like this:
(with-disable-handler-restart (handler) (with-hook-restart (apply handler args)))

Pattern to recognize: Macro call that should be transformed by with-disable-handler-restart

Macro definition:
(defmacro with-disable-handler-restart ((handler) &body body)
  "This is intended to wrap all handler executions."
  `(restart-case
       (progn ,@body)
     (disable-handler ()
       :report
       (lambda (stream)
         (format stream "Disable handler ~a which causes the error." ,handler))
       (disable-hook *hook* ,handler))))

Macroexpansion:
(restart-case (progn (with-hook-restart (apply handler args))) (disable-handler nil :report (lambda (stream) (format stream "Disable handler ~a which causes the error." handler)) (disable-hook *hook* handler)))
Generate a defmacro form that transforms code like this:
(with-temp (d :directory t :debug (<= 1 *debug-prolog*)) (with-temp (input-file :tmpdir d :template "XXXXXX.prolog" :debug (<= 1 *debug-prolog*)) (with-open-file (s input-file :direction :output :if-does-not-exist :error) (dolist (r rules) (print-rule s r))) (run-command-with-debug-print `(,*yap* "-l" ,input-file ,@args) :input input :output output :error error)))

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

Macro definition:
(defmacro with-temp ((var &key directory template (tmpdir "/tmp/") debug) &body body)
  "Create a temporary file, then remove the file by unwind-protect.
Most arguments are analogous to mktemp.
When DIRECTORY is non-nil, creates a directory instead.
DEBUG is a form.
When evaluated to non-nil, it does not remove the directory so that you can investigate what happened inside the directory.
It may be evaluated multiple times."
  (with-gensyms (command)
    `(let ((,var (uiop:run-program
                  (let ((,command (format nil "mktemp --tmpdir='~a' ~@[-d~*~] ~@[~a~]"
                                          ,tmpdir ,directory ,template)))
                    (when ,debug
                      (format *error-output* "~&; ~a~%" ,command))
                    ,command)
                  :output '(:string :stripped t))))
       (unwind-protect
            (progn ,@body)
         (if ,debug
             (format *error-output* "~&; not removing ~a for debugging~%" ,var)
             (uiop:run-program (format nil "rm -rf ~a" (namestring ,var)) :ignore-error-status t))))))

Macroexpansion:
(let ((d (uiop/run-program:run-program (let ((#:command179 (format nil "mktemp --tmpdir='~a' ~@[-d~*~] ~@[~a~]" "/tmp/" t nil))) (when (<= 1 *debug-prolog*) (format *error-output* "~&; ~a~%" #:command179)) #:command179) :output (quote (:string :stripped t))))) (unwind-protect (progn (with-temp (input-file :tmpdir d :template "XXXXXX.prolog" :debug (<= 1 *debug-prolog*)) (with-open-file (s input-file :direction :output :if-does-not-exist :error) (dolist (r rules) (print-rule s r))) (run-command-with-debug-print (sb-int:quasiquote (#S(sb-impl::comma :expr *yap* :kind 0) "-l" #S(sb-impl::comma :expr input-file :kind 0) #S(sb-impl::comma :expr args :kind 2))) :input input :output output :error error))) (if (<= 1 *debug-prolog*) (format *error-output* "~&; not removing ~a for debugging~%" d) (uiop/run-program:run-program (format nil "rm -rf ~a" (namestring d)) :ignore-error-status t))))
Write a Common Lisp macro to handle this code pattern:
(set-arg-list-key-value! nil :key nil)

Problem: Macro call that should be transformed by SET-ARG-LIST-KEY-VALUE!

Macro definition:
(defmacro set-arg-list-key-value! (new ids place &key (test '#'equalper) (key '#'identity))
  `(progn
    (iter (for id in (ensure-list ,ids))
      (setf ,place (set-arg-list-key-value ,new id ,place :test ,test :key ,key)))
    ,place))

Macroexpansion:
(progn
 (iter
   (for id in (ensure-list :key))
   (setf nil
           (set-arg-list-key-value nil id nil :test #'equalper :key
                                   #'identity)))
 nil)
Create a Common Lisp macro for the following transformation. Before code:
(-<> 'some-name)

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

Macro definition:
(defmacro -<> ...)

Macroexpansion:
'some-name
Generate a defmacro form that transforms code like this:
(define-method-combination% standard () ((around (:around)) (before (:before)) (primary () :required t) (after (:after) :order :most-specific-last)) (labels ((call-methods (methods) (mapcar (curry #'list 'call-method) methods)) (call-primary (primary) `(call-method ,(first primary) ,(rest primary))) (call-before (before form) (if before `(progn ,@(call-methods before) ,form) form)) (call-after (after form) (if after `(multiple-value-prog1 ,form ,@(call-methods after)) form)) (call-around (around form) (if around `(call-method ,(first around) (,@(rest around) (make-method ,form))) form))) (->> (call-primary primary) (call-before before) (call-after after) (call-around around))))

Pattern to recognize: Macro call that should be transformed by define-method-combination%

Macro definition:
(defmacro define-method-combination% (name &rest args)
  "Define a method-combination for the static-dispatch system only.

   Arguments are of the same form as to CL:DEFINE-METHOD-COMBINATION."

  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (setf
      (combination-function ',name)
      ,(match args
         ((or nil
              (list* (and (type symbol) (not nil)) _))

          (destructuring-bind (&key (operator name) (identity-with-one-argument nil))
              args

            `(method-combination-function-short ,name ,operator ,identity-with-one-argument)))

         (_
          `(method-combination-function ,name ,@args))))))

Macroexpansion:
(eval-when (:compile-toplevel :load-toplevel :execute) (setf (combination-function (quote standard)) (method-combination-function standard nil ((around (:around)) (before (:before)) (primary nil :required t) (after (:after) :order :most-specific-last)) (labels ((call-methods (methods) (mapcar (curry (function list) (quote call-method)) methods)) (call-primary (primary) (sb-int:quasiquote (call-method #S(sb-impl::comma :expr (first primary) :kind 0) #S(sb-impl::comma :expr (rest primary) :kind 0)))) (call-before (before form) (if before (sb-int:quasiquote (progn #S(sb-impl::comma :expr (call-methods before) :kind 2) #S(sb-impl::comma :expr form :kind 0))) form)) (call-after (after form) (if after (sb-int:quasiquote (multiple-value-prog1 #S(sb-impl::comma :expr form :kind 0) #S(sb-impl::comma :expr (call-methods after) :kind 2))) form)) (call-around (around form) (if around (sb-int:quasiquote (call-method #S(sb-impl::comma :expr (first around) :kind 0) (#S(sb-impl::comma :expr (rest around) :kind 2) (make-method #S(sb-impl::comma :expr form :kind 0))))) form))) (->> (call-primary primary) (call-before before) (call-after after) (call-around around))))))
Define a macro that eliminates this repetitive pattern:
(macroexpand-convention (form) (destructuring-case form ((progn &body body) `(progn ,@(apply-at-macro-to-multiple-forms at-macro-form body))) ((eval-when situations &body body) `(eval-when ,situations ,@(apply-at-macro-to-multiple-forms at-macro-form body))) ((locally &body body) (multiple-value-bind (body declarations) (parse-body body) `(locally ,@declarations ,@(apply-at-macro-to-multiple-forms at-macro-form body)))) (((macrolet symbol-macrolet) bindings &body body) (multiple-value-bind (body declarations) (parse-body body) `(,(first form) ,bindings ,@declarations ,@(apply-at-macro-to-multiple-forms at-macro-form body)))) ((otherwise &rest _) (declare (ignore _)) form)))

Issue: Macro call that should be transformed by macroexpand-convention

Macro definition:
(defmacro macroexpand-convention ((original-form) &body body)
  "Executes BODY as like `progn' and return the result as the first
value.  As the second value, returns a boolean whether the result is
not same with ORIGINAL-FORM or not.
This macro is designed to follow the `macroexpand' convention."
  `(multiple-value-bind (result expanded-p)
       (progn ,@body)
     (values result
             (or expanded-p
                 (not (eq result ,original-form))))))

Macroexpansion:
(multiple-value-bind (result expanded-p) (progn (destructuring-case form ((progn &body body) (sb-int:quasiquote (progn #S(sb-impl::comma :expr (apply-at-macro-to-multiple-forms at-macro-form body) :kind 2)))) ((eval-when situations &body body) (sb-int:quasiquote (eval-when #S(sb-impl::comma :expr situations :kind 0) #S(sb-impl::comma :expr (apply-at-macro-to-multiple-forms at-macro-form body) :kind 2)))) ((locally &body body) (multiple-value-bind (body declarations) (parse-body body) (sb-int:quasiquote (locally #S(sb-impl::comma :expr declarations :kind 2) #S(sb-impl::comma :expr (apply-at-macro-to-multiple-forms at-macro-form body) :kind 2))))) (((macrolet symbol-macrolet) bindings &body body) (multiple-value-bind (body declarations) (parse-body body) (sb-int:quasiquote (#S(sb-impl::comma :expr (first form) :kind 0) #S(sb-impl::comma :expr bindings :kind 0) #S(sb-impl::comma :expr declarations :kind 2) #S(sb-impl::comma :expr (apply-at-macro-to-multiple-forms at-macro-form body) :kind 2))))) ((otherwise &rest _) (declare (ignore _)) form))) (values result (or expanded-p (not (eq result form)))))