我有这些功能:
(def i (atom {})) ;incremented/calculated file stats
(defn updatei [n fic fos]
(swap! i conj {(keyword n)[fic fos]}))
(defn calcfo [fo fi fis]
(if-let [n (get @i fo)] ;find a previous record?
(let [fic (inc (first n)), fos (+ fis (second n))] ;increment the stats
(updatei fo fic fos))
(let [fic 1, fos fis] ;if not then: first calc recorded
(updatei fo fic fos))))
如何编写 (updatei fo fic fos) 一次,而不是在函数中列出两次?难道有什么 secret 或者让我不知道的吗?
-假设代码-
(defn calcfo [fo fi fis]
(if-let [n (get @i fo)] ;find a previous record?
(let [fic (inc (first n)), fos (+ fis (second n))] ;increment the stats
(or-let [fic 1, fos fis] ;if not then: first calc recorded
(updatei fo fic fos)))))
或者我是否过于强制而不是功能性地考虑这个问题?
编辑:
我认为这对我来说最有意义:
(defn calcfo [fo fis]
(apply updatei fo
(if-let [[rfc rfos] (get @i fo)] ;find a previous record?
[(inc rfc) (+ rfos fis)] ;increment the stats
[1 fis]) ;if not then: first calc recorded
))
感谢您的精彩解答!
最佳答案
重新安排可能会有所帮助
(defn calcfo [fo fi fis]
(apply updatei fo
(if-let [n (get @i fo)]
[(inc (first n)), (+ fis (second n))]
[1, fis] )))
关于clojure - 如何使用 if-let 中的嵌套 let 编写一次函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578805/