Skip to content

Instantly share code, notes, and snippets.

@fogus
Last active January 19, 2021 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogus/119adfe60951e36ad38a5f1d35c4ea80 to your computer and use it in GitHub Desktop.
Save fogus/119adfe60951e36ad38a5f1d35c4ea80 to your computer and use it in GitHub Desktop.
;; Basis
(fn [& {:keys [a b]}] (+ a b))
;; gen param lists approach
(defn param-gen-style
([a-map]
(let [{:keys [a b]} a-map]
(+ a b)))
([a-key a-val & kvs]
(let [{:keys [a b]} (concat (list a-key a-val) kvs)]
(+ a b))))
(param-gen-style {:a 1 :b 2})
;;=> 3
(param-gen-style :a 1 :b 2)
;;=> 3
;; gen map check approach
(defn map-check-style
[& kvs]
(let [maybe-map (if (map? (first kvs)) (first kvs) kvs)
{:keys [a b]} maybe-map]
(+ a b)))
(map-check-style {:a 1 :b 2})
;;=> 3
(map-check-style :a 1 :b 2)
;;=> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment