4
votes

When you write higher-order functions in Clojurescript, it is actually possible to omit parameters for a passed-in function.

For instance, the following is legal Clojurescript code but illegal Clojure code:

(def x (atom 5))

(swap! x (fn [] 6))

The higher-order "swap!" function expects a function that takes one parameter, but you can omit it and the program will still compile/run 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 a Clojurescript limitation? Any opinions?

Thanks for your thoughts!

1
I posted an answer, but I'm not sure that it's what you're asking about. - ErikR
Not really answering your question, but for this particular case, you should use reset! instead of swap!. It does what you want. - levand

1 Answers

9
votes

To me (fn [_] 6) looks very idiomatic and not any more obscure than (fn [] 6). It's even more expressive because it explicitly states that the argument is ignored.

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


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