# Method map()

Method map

  ``mixed` map(`mixed` `arr`, `void`|`mixed` `fun`, `mixed` ... `extra`)`

Description

  Applies `fun` to the elements in `arr` and collects the results.

  `arr` is treated as a set of elements, as follows:

  array
  multiset
  string

    `fun` is applied in order to each element. The results are collected, also in order, to a value of the same type as `arr`, which is returned.

  mapping

    `fun` is applied to the values, and each result is assigned to the same index in a new mapping, which is returned.

  program

    The program is treated as a mapping containing the identifiers that are indexable from it and their values.

  object

    If there is a lfun::cast method in the object, it's called to try to cast the object to an array, a mapping, or a multiset, in that order, which is then handled as described above.

  `fun` is applied in different ways depending on its type:

  function

    `fun` is called for each element. It gets the current element as the first argument and `extra` as the rest. The result of the call is collected.

  object

    `fun` is used as a function like above, i.e. the lfun::`() method in it is called.

  multiset
  mapping

    `fun` is indexed with each element. The result of that is collected.

  "zero or left out"

    Each element that is callable is called with `extra` as arguments. The result of the calls are collected. Elements that aren't callable gets zero as result.

  string

    Each element is indexed with the given string. If the result of that is zero then a zero is collected, otherwise it's called with `extra` as arguments and the result of that call is collected.

    This is typically used when `arr` is a collection of objects, and `fun` is the name of some function in them.

Note

  The function is never destructive on `arr`.

See also

  filter(), enumerate(), `foreach()`