Merge branch 'master' into pr/611

This commit is contained in:
Tuukka Turto 2014-06-12 22:58:04 +03:00
commit f8e48628ea

View File

@ -305,7 +305,7 @@ Some example usage:
Yeah, really! Yeah, really!
;; assuming that (side-effect) is a function that we want to call for each ;; 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) => (list-comp (do (side-effect x)
... (if (< x 5) (* 2 x) ... (if (< x 5) (* 2 x)
... (* 4 x))) ... (* 4 x)))
@ -416,7 +416,7 @@ Parameters may have following keywords in front of them:
arguments may be specified after this one. arguments may be specified after this one.
The following code example defines a function that can be given 0 to n 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. every even number.
.. code-block:: clj .. code-block:: clj
@ -1293,23 +1293,48 @@ file is automatically closed after it has been processed.
with-decorator with-decorator
-------------- --------------
`with-decorator` is used to wrap a function with another. The function performing `with-decorator` is used to wrap a function with another. The function
decoration should accept a single value, the function being decorated and return performing decoration should accept a single value, the function being
a new function. `with-decorator` takes two parameters, the function performing decorated and return a new function. `with-decorator` takes a minimum
decoration and the function being decorated. of two parameters, the function performing decoration and the function
being decorated. More than one decorator function can be applied, they
In the following example, `inc-decorator` is used to decorate function `addition` will be applied in order from outermost to innermost, ie. the first
with a function that takes two parameters and calls the decorated function with decorator will be the outermost one & so on. Decorators with arguments
values that are incremented by 1. When decorated `addition` is called with values are called just like a function call.
1 and 1, the end result will be 4 (1+1 + 1+1).
.. code-block:: clj .. 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)))) ... (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))) => (with-decorator inc-decorator (defn addition [a b] (+ a b)))
=> (addition 1 1) => (addition 1 1)
4 4
=> (with-decorator inc2-decorator inc-decorator
... (defn addition [a b] (+ a b)))
=> (addition 1 1)
8
.. _with-gensyms: .. _with-gensyms: