Hello Clojurists,
In other languages there is a known concept of using “opts” map/vector as last (and optional) argument for functions that would have to have too many arguments without it. I looked at Clojure community style guide and it is mentioning opts but without any details about it.
I’m not sure what is the best description of opts
so I’ll explain it by the following example:
(defn get-car-wheel [type opts]
(if-let [number (:number-of-whells opts)]
number
(if (= type :truck) 6 4)))
(defn get-engine [opts]
(if (:premium opts)
{:type :fast :horse-power 100}
{:type :standard :horse-power 80}))
(defn make-car [type opts]
(println "Making" type "car")
{:type type
:wheels (get-car-wheel type opts)
:engine (get-engine opts)})
(make-car :truck {})
;; => {:type :truck,
;; :wheels 6
;; :engine {:type :standard, :horse-power 80}}
(make-car :truck {:number-of-whells 8})
;; => {:type :truck
;; :wheels 8
;; :engine {:type :standard, :horse-power 80}}
(make-car :sport-car {:premium true})
;; => {:type :sport-car
;; :wheels 4
;; :engine {:type :fast, :horse-power 100}}
As you can see, the main function make-car
doesn’t understand opts
and doesn’t have to have a use for it. But other functions may understand some parameters in opts
. And of course those other functions might be functions in function in function…
Could you please point me to some library/app that utilizes such concept so I don’t have to reinvent the wheel?
Thank you.
Adam