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!
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With