1.2.0 permalink Arrow_down_16x16
  • (defnk fn-name & fn-tail)
Define a function accepting keyword arguments. Symbols up to the first
keyword in the parameter list are taken as positional arguments. Then
an alternating sequence of keywords and defaults values is expected. The
values of the keyword arguments are available in the function body by
virtue of the symbol corresponding to the keyword (cf. :keys destructuring).
defnk accepts an optional docstring as well as an optional metadata map.

1 Example top

  • user> (use 'clojure.contrib.def)
    nil
    user> (defnk f [:b 43] (inc b))
    #'user/f
    user> (f)
    44
    user> (f :b 100)
    101
    user=> (defnk with-positional [foo :b 43] (+ foo (inc b)))
    #'user/with-positional
    user=> (with-positional 5 :b 1)
    7
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/def.clj:122 top

(defmacro defnk
 "Define a function accepting keyword arguments. Symbols up to the first
 keyword in the parameter list are taken as positional arguments.  Then
 an alternating sequence of keywords and defaults values is expected. The
 values of the keyword arguments are available in the function body by
 virtue of the symbol corresponding to the keyword (cf. :keys destructuring).
 defnk accepts an optional docstring as well as an optional metadata map."
 [fn-name & fn-tail]
 (let [[fn-name [args & body]] (name-with-attributes fn-name fn-tail)
       [pos kw-vals]           (split-with symbol? args)
       syms                    (map #(-> % name symbol) (take-nth 2 kw-vals))
       values                  (take-nth 2 (rest kw-vals))
       sym-vals                (apply hash-map (interleave syms values))
       de-map                  {:keys (vec syms)
                                :or   sym-vals}]
   `(defn ~fn-name
      [~@pos & options#]
      (let [~de-map (apply hash-map options#)]
        ~@body))))
Vars in clojure.contrib.def/defnk: defmacro let apply concat defn hash-map list seq vector
Used in 0 other vars

Comments top

No comments for defnk. Log in to add a comment.