如何将一系列添加到列表的调用重构为 let?
How to refactor a sequence of add-to-list calls into a let?
我主要是想重构以下内容:
(eval-after-load 'find-file-in-project
'(progn
(add-to-list 'ffip-patterns "*.clj")
(add-to-list 'ffip-patterns "*.erb")
(add-to-list 'ffip-patterns "*.haml")
(add-to-list 'ffip-patterns "Gemfile*")
(add-to-list 'ffip-patterns "*.css")
(add-to-list 'ffip-patterns "*.scss")
(add-to-list 'ffip-patterns "*.sass")
(add-to-list 'ffip-patterns "*.rake")
(add-to-list 'ffip-patterns "*.thor")
(add-to-list 'ffip-patterns "Capfile")
(add-to-list 'ffip-patterns "*.gemspec")
(add-to-list 'ffip-patterns "*.yml")
))
进入一些不错的地方,我定义了一次文件扩展名列表,然后将它们转换为 ffip-patterns
变量。
如果您不担心列表中的重复项(add-to-list
检查),您可以使用 append
:
添加元素
(eval-after-load 'find-file-in-project
'(setq ffip-patterns (append (list "*.clj" "*.erb") ffip-patterns)))
否则,您可以使用 dolist
:
遍历扩展
(eval-after-load 'find-file-in-project
'(dolist (pattern (list "*.clj" "*.erb"))
(add-to-list 'ffip-patterns pattern)))
我主要是想重构以下内容:
(eval-after-load 'find-file-in-project
'(progn
(add-to-list 'ffip-patterns "*.clj")
(add-to-list 'ffip-patterns "*.erb")
(add-to-list 'ffip-patterns "*.haml")
(add-to-list 'ffip-patterns "Gemfile*")
(add-to-list 'ffip-patterns "*.css")
(add-to-list 'ffip-patterns "*.scss")
(add-to-list 'ffip-patterns "*.sass")
(add-to-list 'ffip-patterns "*.rake")
(add-to-list 'ffip-patterns "*.thor")
(add-to-list 'ffip-patterns "Capfile")
(add-to-list 'ffip-patterns "*.gemspec")
(add-to-list 'ffip-patterns "*.yml")
))
进入一些不错的地方,我定义了一次文件扩展名列表,然后将它们转换为 ffip-patterns
变量。
如果您不担心列表中的重复项(add-to-list
检查),您可以使用 append
:
(eval-after-load 'find-file-in-project
'(setq ffip-patterns (append (list "*.clj" "*.erb") ffip-patterns)))
否则,您可以使用 dolist
:
(eval-after-load 'find-file-in-project
'(dolist (pattern (list "*.clj" "*.erb"))
(add-to-list 'ffip-patterns pattern)))