xiaohongshu
xiaohongshu
cl-macro-design
マクロ設計のベストプラクティスを適用。マクロ作成・レビュー時に使用
Full skill instructions
Based on Google Common Lisp Style Guide (@references/google-common-lisp-style-guide.md)
;; BAD - パフォーマンスのためにマクロ化
(defmacro fast-add (a b)
`(+ ,a ,b))
;; GOOD - inline宣言で同等の効果
(declaim (inline fast-add))
(defun fast-add (a b)
(+ a b))
;; BAD - 変数捕捉の危険
(defmacro with-timing-bad (&body body)
`(let ((start (get-internal-real-time)))
,@body
(- (get-internal-real-time) start)))
;; GOOD - gensymで安全
(defmacro with-timing (&body body)
(let ((start (gensym "START")))
`(let ((,start (get-internal-real-time)))
,@body
(- (get-internal-real-time) ,start))))
;; Alexandriaのwith-gensymsを使用
(defmacro with-retry ((var max-attempts) &body body)
(with-gensyms (attempt result success)
`(loop for ,attempt from 1 to ,max-attempts
do (multiple-value-bind (,result ,success)
(ignore-errors ,@body)
(when ,success
(return ,result)))
finally (error "Max attempts exceeded"))))
;; BAD - 引数が2回評価される
(defmacro square-bad (x)
`(* ,x ,x))
(square-bad (incf n)) ; nが2回インクリメントされる!
;; GOOD - 一度だけ評価
(defmacro square (x)
(let ((v (gensym)))
`(let ((,v ,x))
(* ,v ,v))))
;; once-only ユーティリティを使用 (Alexandria)
(defmacro square (x)
(once-only (x)
`(* ,x ,x)))
;; 1段階展開
(macroexpand-1 '(with-timing (heavy-computation)))
;; 完全展開
(macroexpand '(with-timing (heavy-computation)))
;; プリティプリント
(pprint (macroexpand-1 '(your-macro args)))
;; Emacs/SLIME: C-c C-m (slime-expand-1)
;; カーソル位置のマクロを展開表示
マクロは構文処理のみを担当し、セマンティクスは補助関数に委譲する。
;; マクロは薄いラッパー
(defmacro with-foo (() &body body)
`(call-with-foo (lambda () ,@body)))
;; セマンティクスは関数で実装
(defun call-with-foo (thunk)
(setup-foo)
(unwind-protect
(funcall thunk)
(teardown-foo)))
利点:
(defmacro with-open-database ((var connection-string) &body body)
(let ((db (gensym "DB")))
`(let ((,db (connect-database ,connection-string)))
(unwind-protect
(let ((,var ,db))
,@body)
(disconnect-database ,db)))))
(defmacro do-lines ((var stream &optional result) &body body)
(let ((s (gensym "STREAM")))
`(let ((,s ,stream))
(loop for ,var = (read-line ,s nil nil)
while ,var
do (progn ,@body)
finally (return ,result)))))
(defmacro define-api-endpoint (name (method path) &body body)
`(progn
(defun ,name (request)
,@body)
(register-endpoint ,method ,path #',name)
',name))
;; 使用
(define-api-endpoint get-users (:get "/api/users")
(fetch-all-users))
(defmacro debug-log (format-string &rest args)
#+debug
`(format *debug-io* ,(concatenate 'string "[DEBUG] " format-string "~%")
,@args)
#-debug
nil)
評価される形式には -form サフィックスを付ける:
;; 明確: condition-form は評価される式
(defmacro when-available (condition-form &body body)
`(when ,condition-form
,@body))
;; 例外: body, end は慣例的なので -form 不要
将来の拡張に備えて空のパラメータリストを置く:
;; BAD - 拡張が困難
(defmacro with-lights-on (&body body)
...)
;; GOOD - 後からオプションを追加可能
(defmacro with-lights-on (() &body body)
...)
;; 拡張例
(defmacro with-lights-on ((&key color intensity) &body body)
...)
引数の複数評価を防止:
(defmacro double (value-form)
(alexandria:once-only (value-form)
`(+ ,value-form ,value-form)))
;; BAD - 関数で十分
(defmacro add-one (x)
`(+ ,x 1))
;; GOOD
(defun add-one (x)
(+ x 1))
;; BAD - マクロ内に大量のロジック
(defmacro complex-operation (...)
`(progn
;; 100行のコード...
))
;; GOOD - ヘルパー関数に分離
(defun %complex-operation-impl (...)
;; 実装
)
(defmacro complex-operation (...)
`(%complex-operation-impl ...))
;; BAD - 副作用が分かりにくい
(defmacro with-user (user &body body)
`(let ((*current-user* ,user))
(log-user-action ,user) ; 隠れた副作用
,@body))
;; GOOD - 明示的
(defmacro with-user (user &body body)
"Bind *CURRENT-USER* and log the action."
`(let ((*current-user* ,user))
,@body))
;; ログは呼び出し側で明示
(with-user user
(log-user-action user)
(do-something))
;; 禁止: プロジェクト外に露出するリーダーマクロ
(set-macro-character #\[ ...) ; 危険
;; 許可される場合のみ: named-readtables で制御
(named-readtables:in-readtable :my-syntax)
;; 禁止: セキュリティ脆弱性
(eval (read-from-string user-input))
;; 許可: 開発ツール、ビルド基盤のみ
;; BAD - バッククォート内でロジック
(defmacro complex (&body body)
`(progn
,@(if (some-condition body)
(transform-body body)
body)))
;; GOOD - ロジックを外に出す
(defmacro complex (&body body)
(let ((processed (if (some-condition body)
(transform-body body)
body)))
`(progn ,@processed)))
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience