The important class of functionals in practical programming in language Lisp is derivated by displaying functions or map-functions (mapping function). Map-functionals are functions which to some extent display (map) the list (vector) in new element or generate the by-effect. Call of map-function looks like:
(obj-seq map fn argseq1 … argseqn)
Here obj-seq argseq1 … argseqn - (vector) lists, and fn - function from n arguments. As a rule, map-function is applied to function without arguments.
(obj-seq map fn)
The list (vector) consisting of results of application of function will be result of these calculations. All calculations are made in a parallel way. For example, in case of one object box following correspondence turns out:
('(x1 x2 … xn) map fn)
≡
(nil list ('x1 fn) ('x2 fn) … ('xn fn))
As value of a functional the list constructed of results of calls of functional argument map comes back. We will give an example:
>('l set '(1 2 3)) (1 2 3) >(l map 'oddp) (1 false 3) >(l map '(lambda () (nil list (nil progn (cout writeln this) this)))) 2 1 ; parallel fulfilment 3 ((1) (2) (3)) >(nil defmethod pair () (nil list this 'x)) (lambda () (nil list this 'x)) >(l map 'pair) ((1 x) (2 x) (3 x))
Used above function (oddp, pair) were functions without arguments, i.e. each time they deal with one unit. If functions map have been transferred functional argument fn with a considerable quantity of parametres and the map-functional would have an appropriate quantity of parametres-lists.
In the call brought more low, for example, functional argument map is function cons which demands two arguments. Call map builds the list of pairs of appropriate units of two viewed lists:
>(l map '(lambda (x) (nil cons this x)) '(a b c)) ((1 . a) (2 . b) (3 . c)) >(#(1 2 3) map '(lambda (x) (nil list this x)) #(a b c)) #((1 a) (2 b) (3 c))
map it is possible to define through used in Lisp applying functional. For a case of functional argument without parametres function map would look as follows:
(nil defmethod map (fn) (nil if (nil consp this) (nil cons (nil funcall (this first) fn) ((this rest) map fn))))
The functional map is used for programming of cycles of special sort and in definition of other functions as with their help it is possible to reduce record of repeating calculations.
Function mapc also is similar to function map, but differs that does not gather and does not unite results. Arising results are simply lost. A pseudo-functional mapc first of all use for by-effect reception.