Skip to content

Instantly share code, notes, and snippets.

@nipra
Created August 26, 2011 18:24
Show Gist options
  • Save nipra/1174053 to your computer and use it in GitHub Desktop.
Save nipra/1174053 to your computer and use it in GitHub Desktop.
Python **kwargs like functionality when doing function call
;; Just for fun!
;; If Clojure were like Python, I could have used (asdf **{:a 1 :b 2 :c 3}) when calling asdf
user> (defn asdf
[& {:keys [a b c]}]
[a b c])
#'user/asdf
user> (defmacro call**
[func kw-map]
`(~func ~@(mapcat identity kw-map)))
#'user/call**
user> (call** asdf {:a 1 :b 2 :c 3})
[1 2 3]
user> (macroexpand '(call** asdf {:a 1 :b 2 :c 3}))
(asdf :a 1 :b 2 :c 3)
user>
@ghoseb
Copy link

ghoseb commented Aug 27, 2011

In my opinion this doesn't need to be a macro at all. I would write this like this -

(defn apply*
  "Applies fn f to the key/value list indicated by argmap."
  [f argmap]
  (let [args (mapcat identity argmap)]
    (apply f args)))

(apply* asdf {:a 1 :b 2 :c 3})
;=> [1 2 3]

@citizen428
Copy link

IMHO, @ghoseb's version is more idiomatic.

@nipra
Copy link
Author

nipra commented Aug 27, 2011

@citizen428 +1 I was hell bent to create a macro. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment