我有这些功能:
(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) 一次,而不是在函数中列出两次?有什么我不知道的秘密吗?
-假设代码-
(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
))
感谢您的精彩回答!