gpt4 book ai didi

将函数与 Let 关联的 Clojure 最佳实践

转载 作者:行者123 更新时间:2023-12-02 16:09:43 25 4
gpt4 key购买 nike

我正在另一个函数中定义一个函数。以下方法之一是否比另一种更好或更优选?

(let [hey (println "hey there")] hey)

(let [hey (fn [] (println "hey here"))] (hey))

最佳答案

第一个版本不会按照您可能希望的方式工作。它在 let 期间被评估一次,并且 hey 将绑定(bind)到值 nil。即 hey 的值不会是一个函数。

第二个很好,而且很容易阅读。其他方法:

(let [hey0 #(println "hey0")] (hey0))

(letfn [(hey1 [] (println "hey1"))] (hey1))

据我所知,没有真正的使用规则。我仅对非常短的函数使用 #() 读取器宏形式,如果我一起定义一堆内部函数,则使用 letfn

关于将函数与 Let 关联的 Clojure 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23614315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com