Idiomatic no-op / "pass"

What is the (most) idiomatic representation of Clojure no-op ? I.e.

 (def r (ref {})) ... (let [der @r] (match [(:a der) (:b der)] [nil nil] (do (fill-in-a) (fill-in-b)) [_ nil] (fill-in-b) [nil _] (fill-in-a) [_ _] ????)) 

Python has pass . What should I use in Clojure?

ETA: I ask mainly because I came across places ( cond , for example) where nothing comes up that causes an error. I understand that "most" of the time, the equivalent of pass not required, but when that is the case, I would like to know what is the most Clojuric.

+6
source share
3 answers

I see a keyword :default , used in cases like this quite often.
It has a good recognition property in outputs and / or logs. Thus, when you see a log line, for example: “process completed: default”, it is obvious that nothing happened. This exploits the fact that keywords are true in Clojure, so the default value will be considered successful.

+10
source

Clojure has no “statements”, but there are many ways to “do nothing”. An empty block (do) literally indicates that it "does nothing" and evaluates to nil. In addition, I agree with the comment that the question itself indicates that you are not using Clojure in an idiomatic manner, regardless of this particular stylistic question.

+5
source

The most similar thing that I can think of in Clojure, to the "statement that does nothing" from imperative programming, will be a function that does nothing. There are several built-in modules that can help you: identity is a function with one argument that simply returns its argument, and constantly is a higher order function that takes a value and returns a function that will take any number of arguments and return that value . Both of them are useful as placeholders in situations where you need to pass a function, but you do not want this function to actually perform most of everything. A simple example:

 (defn twizzle [x] (let [f (cond (even? x) (partial * 4) (= 0 (rem x 3)) (partial + 2) :else identity)] (f (inc x)))) 

Rewriting this function as “do nothing” in the default case, although this is possible, will require inconvenient rewriting without using identity .

+4
source

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


All Articles