Dao*_*Wen 11
你可能想尝试在Clojure中有两个函数:mod和rem.它们的正数相同,但负数则不同.以下是文档中的示例:
(mod -10 3) ; => 2
(rem -10 3) ; => -1
Run Code Online (Sandbox Code Playgroud)
更新:
如果你真的想将你的代码转换为Clojure,你需要意识到一个惯用的Clojure解决方案可能看起来不像你的JavaScript解决方案.这是一个很好的解决方案,我认为大致是你想要的:
(defn change [amount]
(zipmap [:quarters :dimes :nickels :pennies]
(reduce (fn [acc x]
(let [amt (peek acc)]
(conj (pop acc)
(quot amt x)
(rem amt x))))
[amount] [25 10 5])))
(change 142)
; => {:pennies 2, :nickels 1, :dimes 1, :quarters 5}
Run Code Online (Sandbox Code Playgroud)
您可以在ClojureDocs上查找任何您无法识别的功能.如果你只是不理解这种风格,那么你可能需要更多使用高阶函数编程的经验.我认为4Clojure是一个很好的起点.