Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are optional "call-back" parameters in Clojurescript frowned upon?

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!

like image 717
drcode Avatar asked Jan 16 '23 08:01

drcode


1 Answers

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.

like image 128
Ivan Koblik Avatar answered Jan 30 '23 21:01

Ivan Koblik