Previous: , Up: Functions   [Contents][Index]


5.9.7 Mapping Functions

A mapping function applies a function to each of a collection of objects. Librep currently has two mapping functions, mapcar and mapc.

Function: mapcar function list

Each element of list is individually applied to the function function. The values returned are made into a new list which is returned.

The function must accept a single argument value.

(mapcar 1+ '(1 2 3 4 5))
    ⇒ (2 3 4 5 6)
Function: mapc function list

Similar to mapcar except that the values returned when each element is applied to the function function are discarded. The value returned is undefined.

This function is generally used where the side effects of calling the function are the important thing, not the results. It is often the most efficient way of traversing all items in a list, for example:

(mapc (lambda (x)
        (print x standard-error)) list)

The two following functions are also mapping functions of a sort. They are variants of the delete function (see Modifying Lists) and use predicate functions to classify the elements of the list which are to be deleted.

Function: delete-if predicate list

This function is a variant of the delete function. Instead of comparing each element of list with a specified object, each element of list is applied to the predicate function predicate. If it returns true then the element is destructively removed from list.

(delete-if stringp '(1 "foo" 2 "bar" 3 "baz"))
    ⇒ (1 2 3)
Function: delete-if-not predicate list

This function does the inverse of delete-if. It applies predicate to each element of list, if it returns false then the element is destructively removed from the list.

(delete-if-not stringp '(1 "foo" 2 "bar" 3 "baz"))
    ⇒ ("foo" "bar" "baz")
Function: remove-if pred list
Function: remove-if-not pred list

Same as delete-if / delete-if-not respectively, but returns a new copy, instead of destructively. Defined in rep.data.

The filter function is similar to delete-if-not, except that the original list isn’t modified, a new list is created.

Function: filter predicate list

Return a new list, consisting of the elements in list which the function predicate returns true when applied to. This function is equivalent to:

(mapcar nconc (mapcar (lambda (x)
                        (and (predicate x) (list x)))
                      list))

Previous: , Up: Functions   [Contents][Index]