Clojure Best Practice for Associating a Function with Let

I define a function inside another function. Is one of the following approaches better or preferable to the other?

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

or

(let [hey (fn [] (println "hey here"))] (hey))
+4
source share
1 answer

The first version will not work the way you probably want. It is evaluated once during let, and heywill be bound to the value nil. that is, the value heywill not be a function.

The second is wonderful and easy to read. Other approaches:

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

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

There are no real rules for using them that I know of. I use the reader macro #()only for very short functions and letfnif I define a collection of internal functions together.

+6
source

Source: https://habr.com/ru/post/1540258/


All Articles