Merge branch 'master' into pr/611
This commit is contained in:
commit
f8e48628ea
@ -305,7 +305,7 @@ Some example usage:
|
||||
Yeah, really!
|
||||
|
||||
;; assuming that (side-effect) is a function that we want to call for each
|
||||
;; and every value in the list, but which return values we do not care
|
||||
;; and every value in the list, but whose return value we do not care about
|
||||
=> (list-comp (do (side-effect x)
|
||||
... (if (< x 5) (* 2 x)
|
||||
... (* 4 x)))
|
||||
@ -416,7 +416,7 @@ Parameters may have following keywords in front of them:
|
||||
arguments may be specified after this one.
|
||||
|
||||
The following code example defines a function that can be given 0 to n
|
||||
numerical parameters. It then sums every odd number and substracts
|
||||
numerical parameters. It then sums every odd number and subtracts
|
||||
every even number.
|
||||
|
||||
.. code-block:: clj
|
||||
@ -1293,23 +1293,48 @@ file is automatically closed after it has been processed.
|
||||
with-decorator
|
||||
--------------
|
||||
|
||||
`with-decorator` is used to wrap a function with another. The function performing
|
||||
decoration should accept a single value, the function being decorated and return
|
||||
a new function. `with-decorator` takes two parameters, the function performing
|
||||
decoration and the function being decorated.
|
||||
|
||||
In the following example, `inc-decorator` is used to decorate function `addition`
|
||||
with a function that takes two parameters and calls the decorated function with
|
||||
values that are incremented by 1. When decorated `addition` is called with values
|
||||
1 and 1, the end result will be 4 (1+1 + 1+1).
|
||||
`with-decorator` is used to wrap a function with another. The function
|
||||
performing decoration should accept a single value, the function being
|
||||
decorated and return a new function. `with-decorator` takes a minimum
|
||||
of two parameters, the function performing decoration and the function
|
||||
being decorated. More than one decorator function can be applied, they
|
||||
will be applied in order from outermost to innermost, ie. the first
|
||||
decorator will be the outermost one & so on. Decorators with arguments
|
||||
are called just like a function call.
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
=> (defn inc-decorator [func]
|
||||
(with-decorator decorator-fun
|
||||
(defn some-function [] ...)
|
||||
|
||||
(with-decorator decorator1 decorator2 ...
|
||||
(defn some-function [] ...)
|
||||
|
||||
(with-decorator (decorator arg) ..
|
||||
(defn some-function [] ...)
|
||||
|
||||
|
||||
|
||||
In the following example, `inc-decorator` is used to decorate function
|
||||
`addition` with a function that takes two parameters and calls the
|
||||
decorated function with values that are incremented by 1. When
|
||||
decorated `addition` is called with values 1 and 1, the end result
|
||||
will be 4 (1+1 + 1+1).
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
=> (defn inc-decorator [func]
|
||||
... (fn [value-1 value-2] (func (+ value-1 1) (+ value-2 1))))
|
||||
=> (defn inc2-decorator [func]
|
||||
... (fn [value-1 value-2] (func (+ value-1 2) (+ value-2 2))))
|
||||
|
||||
=> (with-decorator inc-decorator (defn addition [a b] (+ a b)))
|
||||
=> (addition 1 1)
|
||||
4
|
||||
=> (with-decorator inc2-decorator inc-decorator
|
||||
... (defn addition [a b] (+ a b)))
|
||||
=> (addition 1 1)
|
||||
8
|
||||
|
||||
|
||||
.. _with-gensyms:
|
||||
|
Loading…
Reference in New Issue
Block a user