Clojurescript frown options optionally?

When you write higher-order functions in Clojurescript, you can actually omit the parameters for the passed function.

For example, the following legitimate Clojurescript code, but illegal Clojure code:

(def x (atom 5))

(swap! x (fn [] 6))

Higher Swap! the function expects a function that takes one parameter, but you can omit it, and the program will still compile / execute just fine.

Would it be considered bad form to use this ability if it makes my Clojurescript code cleaner? Or is it just abuse of Clojurescript restrictions? Any opinions?

Thanks for your thoughts!

+4
source share
1 answer

For me, (fn [_] 6) looks very idiomatic and no more obscure than (fn [] 6) . This is even more expressive because it explicitly states that the argument is ignored.

Another advantage of writing the full (correct) form is the portability of your code.


EDIT: By the way, your example can be rewritten using constantly: (swap! x (constantly 6)) . constantly creates a function that takes any number of arguments and always returns the argument passed to constantly .

+9
source

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


All Articles