From 4b00a84f9f0dbf9333e0a88ef7f28d210649f3cf Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sun, 15 Mar 2015 17:22:48 -0700 Subject: [PATCH] documentation for &kwonly --- docs/language/api.rst | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/language/api.rst b/docs/language/api.rst index 40c8000..625ebc0 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -441,6 +441,41 @@ Parameters may have the following keywords in front of them: => (zig-zag-sum 1 2 3 4 5 6) -3 +&kwonly + .. versionadded:: 0.12.0 + + Parameters that can only be called as keywords. Mandatory + keyword-only arguments are declared with the argument's name; + optional keyword-only arguments are declared as a two-element list + containing the argument name followed by the default value (as + with `&optional` above). + + .. code-block:: clj + + => (defn compare [a b &kwonly keyfn [reverse false]] + ... (let [[result (keyfn a b)]] + ... (if (not reverse) + ... result + ... (- result)))) + => (apply compare ["lisp" "python"] + ... {"keyfn" (fn [x y] + ... (reduce - (map (fn [s] (ord (first s))) [x y])))}) + -4 + => (apply compare ["lisp" "python"] + ... {"keyfn" (fn [x y] + ... (reduce - (map (fn [s] (ord (first s))) [x y]))) + ... "reverse" true}) + 4 + + .. code-block:: python + + => (compare "lisp" "python") + Traceback (most recent call last): + File "", line 1, in + TypeError: compare() missing 1 required keyword-only argument: 'keyfn' + + Availability: Python 3. + .. _defn-alias / defun-alias: defn-alias / defun-alias