Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure best practice for nested let

Is is good practice to use Clojure nested let in the following way, or is it confusing ?

(defn a-fun [config]
  (let [config (-> config (parse) (supply-defaults))]
  ;; do something with config
  ))

I noticed I have this pattern of parsing/checking/validating things quite often in my input functions that talk to the external world (in this case a Clojurescript library that exposes public functions, but I also had Compojure routes with this same feeling).

Is it confusing, because one has to understand the rules for bindings visibility (not sure what the exact wording is) ?

What would be the idiomatic way to do it ? Change the config name to parsed-config, put it in another function, something else completely ?

like image 370
nha Avatar asked Dec 06 '22 20:12

nha


1 Answers

I would reach for this idiom when

  • the rebinding is the same kind of thing and
  • you want to make clear that the local binding supersedes the global one.

For example

(defn fact [n]
  (loop [n n, answer 1]
    (if (pos? n)
      (recur (dec n) (* answer n))
      answer)))

This also stops you using the global binding by accident, as I was prone to do.

like image 149
Thumbnail Avatar answered Jan 15 '23 19:01

Thumbnail