我有一个这样开始的函数:
(defn data-one [suser]
(def suser-first-name
(select db/firstNames
(fields :firstname)
(where {:username suser})))
(def suser-middle-name
(select db/middleNames
(fields :middlename)
(where {:username suser})))
(def suser-last-name
(select db/middleNames
(fields :lastname)
(where {:username suser})))
;; And it just continues on and on...
)
当然,我一点也不喜欢这个。我的代码库中的许多区域都有这种模式重复,我想概括一下。
所以,我想出了以下开始:
(def data-input {:one '[suser-first-name db/firstNames :firstname]
'[suser-middle-name db/middleNames :middlename]
'[suser-last-name db/lastNames :lastname]})
(defpartial data-build [data-item suser]
;; data-item takes the arg :one in this case
`(def (data-input data-item)
(select (data-input data-item)
(fields (data-input data-item))
(where {:username suser}))))
这里真的有几个问题:
- 我如何解构数据输入,以便在 x 未知时创建 x 函数,即。:one 的值是未知的,并且 data-input 中键的数量是未知的。
-- 我在想现在是创建宏的时候,但我以前从未构建过宏,所以我对这个想法犹豫不决。
并且给出一点上下文,函数必须返回要解构的值,但我认为一旦我解决了这个问题,概括所有这些都是可行的:
(defpage "/page-one" []
(let [suser (sesh/get :username)]
(data-one suser)
[:p "Firat Name: "
[:i (let [[{fname :firstname}] suser-first-name]
(format "%s" fname))]
[:p "Middle Name: "
[:i (let [[{mname :emptype}] suser-middle-name]
(format "%s" mname))]
[:p "Last Name: "
[:i (let [[{lname :months}] suser-last-name]
(format "%s" lname))]]))