|
276 | 276 |
|
277 | 277 | (def
|
278 | 278 | ^{:macro true
|
279 |
| - :doc "Define a new function with an optional docstring." |
| 279 | + :doc "Define a new function with an optional docstring. |
| 280 | + |
| 281 | + The function will be interned in the current Namespace as a Var using |
| 282 | + the given `name`. |
| 283 | + |
| 284 | + After the name, an optional mapping of meta attributes may be provided. |
| 285 | + Any metadata values given will be attached to the metadata of the |
| 286 | + interned Var. A few special meta keys change how `defn` emits the |
| 287 | + final Var and function: |
| 288 | + |
| 289 | + - `:decorators` is an optional vector of functions which will wrap |
| 290 | + the final function emitted by `defn`. Like standard Python decorators |
| 291 | + and the `comp` function, decorators are applied to the generated |
| 292 | + function from right to left. |
| 293 | + |
| 294 | + Specify an optional docstring after the metadata map to provide callers |
| 295 | + with additional details about your function, its arguments, and its |
| 296 | + return value. If a docstring is provided, it will be attached to the |
| 297 | + interned Var metadata under the `:doc` key. |
| 298 | + |
| 299 | + Functions may be defined with 1 or more arities. Functions of a single |
| 300 | + arity may be defined with a single vector of 0 or more arguments after |
| 301 | + the optional metadata map. Any forms appearing after the argument |
| 302 | + vector will be part of the function body. It is legal to define a |
| 303 | + function with no body. In this case, your function will always return |
| 304 | + `nil`. |
| 305 | + |
| 306 | + Functions with multiple arities are defined as a series of lists after |
| 307 | + the optional metadata map. Each list must contain an argument vector |
| 308 | + and 0 or more body forms. No arity may share the same number of fixed |
| 309 | + (non-variadic) arguments. There may be at most one variadic arity |
| 310 | + defined per function and the number of fixed arguments to that arity |
| 311 | + must be greater than or equal than the number of fixed arguments of all |
| 312 | + other arities." |
280 | 313 | :arglists '([name & body] [name doc? & body] [name doc? attr-map? & body])}
|
281 | 314 | defn
|
282 | 315 | (fn defn [&env &form name & body]
|
|
291 | 324 | body (if doc
|
292 | 325 | (rest body)
|
293 | 326 | body)
|
294 |
| - fmeta (if (map? (first body)) |
295 |
| - (first body) |
296 |
| - nil) |
| 327 | + fmeta (if (map? (first body)) |
| 328 | + (first body) |
| 329 | + nil) |
297 | 330 | fname (if fmeta
|
298 | 331 | (vary-meta name conj fmeta)
|
299 | 332 | name)
|
300 | 333 | fname (if doc
|
301 | 334 | (vary-meta fname assoc :doc doc)
|
302 |
| - (vary-meta fname conj fmeta)) |
| 335 | + fname) |
303 | 336 | body (if fmeta
|
304 | 337 | (rest body)
|
305 | 338 | body)
|
|
321 | 354 | (throw
|
322 | 355 | (ex-info "Expected an argument vector"
|
323 | 356 | {:found (first body)})))
|
324 |
| - (rest body)))] |
325 |
| - `(def ~fname |
326 |
| - (fn ~fname |
327 |
| - ~@body))))) |
| 357 | + (rest body))) |
| 358 | + |
| 359 | + decorators (:decorators fmeta) |
| 360 | + fn-body (if decorators |
| 361 | + (loop [wrappers (seq (python/reversed decorators)) |
| 362 | + final `(fn ~fname ~@body)] |
| 363 | + (if (seq wrappers) |
| 364 | + (recur (rest wrappers) |
| 365 | + `(~(first wrappers) ~final)) |
| 366 | + final)) |
| 367 | + `(fn ~fname ~@body))] |
| 368 | + `(def ~fname ~fn-body)))) |
328 | 369 |
|
329 | 370 | (defn nth
|
330 | 371 | "Returns the `i`th element of `coll` (0-indexed), if it exists or `nil`
|
|
417 | 458 |
|
418 | 459 | (def
|
419 | 460 | ^{:macro true
|
420 |
| - :doc "Define a new macro like defn. Macro functions are available to the |
421 |
| - compiler during macroexpansion." |
| 461 | + :doc "Define a new macro function. The arguments and syntax of `defmacro` |
| 462 | + are identical to that of `defn`. |
| 463 | + |
| 464 | + When the compiler encounters a new macro function invocation, it |
| 465 | + immediately invokes that function during compilation and substitutes |
| 466 | + the function invocation with the return value of the called macro. |
| 467 | + Macros must return valid syntax at the point they are invoked, |
| 468 | + otherwise the compiler will throw an exception and compilation |
| 469 | + will halt. |
| 470 | + |
| 471 | + Macros created by `defmacro` have access to two implicit arguments |
| 472 | + whose names must not appear in your argument list: |
| 473 | + |
| 474 | + - `&env` is a map of all symbol bindings available to the compiler |
| 475 | + at the point the macro is invoked. |
| 476 | + - `&form` is the original form invoking the macro. This is often |
| 477 | + useful for reading and copying metadata attached to the original |
| 478 | + form." |
422 | 479 | :arglists '([name & body] [name doc? & body] [name doc? attr-map? & body])}
|
423 | 480 | defmacro
|
424 | 481 | (fn defmacro [&env &form name & body]
|
|
482 | 539 | ~@body))
|
483 | 540 |
|
484 | 541 | (defmacro defn-
|
485 |
| - "Define a new private function as by `defn`." |
| 542 | + "Define a new private function as by `defn`. |
| 543 | + |
| 544 | + Private functions are `def`'ed with the `:private` metadata, which makes them |
| 545 | + ineligible for access outside the namespace using `require` or `refer`." |
486 | 546 | [name & body]
|
487 | 547 | `(defn ~(vary-meta name assoc :private true)
|
488 | 548 | ~@body))
|
|
0 commit comments