【发布时间】:2020-01-21 20:54:43
【问题描述】:
我有一个小的 clojure 函数:
(defn split-legal-ref
"Highly specific function that expects one AssessPro map and a map key,
from which book and page will be extracted."
[assess-pro-acct extract-key]
(let [[book page] (cstr/split (extract-key assess-pro-acct) #"-")]
(list (cstr/trim book) (cstr/trim page))))
鉴于:(extract-key assess-pro-acct) #"-"),extract-key 的值为:legal_ref。因此,它从映射中获取像 927-48 这样的单个值,并使用“-”拆分该值。我只需要在没有这些好的价值之一时抓住它。这就是 split 返回 nil 的地方。
所以,我一直试图用以下内容替换原始功能。
(def missing-book 888)
(def missing-page 999)
.
.
.
(defn split-legal-ref
"Highly specific function that expects one AssessPro map and a map key,
from which book and page will be extracted."
[assess-pro-acct extract-key]
(let [[book page] (cstr/split (extract-key assess-pro-acct) #"-")]
(let [[trimBook trimPage] ((if book (cstr/trim book) (missing-book))
(if page (cstr/trim page) (missing-page)))]
(list (trimBook) (trimPage)))))
问题是我一直在害怕
String cannot be cast to clojure.lang.IFn From Small Clojure Function
错误。如何重构此函数以避免错误?
发布答案编辑:
感谢您的回答:
我重新设计了函数来测试字符串中的“-”。如果不存在,则在不存在时使用虚拟“888-99”作为值。
(def missing-book-page "888-99")
.
.
.
(defn split-legal-ref
"Highly specific function that expects one AssessPro map and a map key,
from which book and page will be extracted."
[assess-pro-acct extract-key]
(let [[book page]
(if (.contains "-" (extract-key assess-pro-acct))
(cstr/split (extract-key assess-pro-acct) #"-")
(cstr/split missing-book-page #"-"))]
(list (cstr/trim book) (cstr/trim page))))
【问题讨论】:
标签: clojure