我有一個功能「my-func
」,它處理地圖x
它獲取作爲參數並返回一張地圖。處理過程非常漫長,因此我將其轉移到函數block-with-several-function-calls-on-x
如果傳入地圖包含:special-key
,則應將附加的鍵值對添加到傳出地圖中。什麼是「做額外的東西,當...」的Clojure方式
(defn my-func [x]
(if (contains? x :special-key)
(assoc (block-with-several-function-calls-on-x x)
:extra-key true)
(block-with-several-function-calls-on-x x)))
我真的想保持內my-func
的block-with-several-function-calls-on-x
的代碼。我使用其他編程語言,我只是簡單地將該部分的輸出保存在一個變量中,並且(如有必要)之後添加附加部分並返回值。我被告知使用let
不是「Clojuresque」。
那麼,將代碼傳遞給一個函數(就像我上面做的那樣)是唯一的方法?或者是一個do-additonal-stuff-to-output-of-second-block
-function:
(defn my-func [x]
(do-additional-stuff-to-output-of-second-block
(when (contains? x :special-key)
(assoc :extra-key true))
(block-with-several-function-calls-on-x x)))
「使用讓不是Clojuresque」 - 這是荒謬和搞笑的錯誤,並說這個人不太瞭解Clojure – noisesmith 2014-11-25 15:06:14
也許有一些溝通不暢?例如,讓我們不創建「變量」。無論如何,執行此操作的標準方法是使用let。 – noisesmith 2014-11-25 15:13:05