map function

List map(
  1. List list,
  2. dynamic iteratee(
    1. dynamic element,
    2. int index,
    3. List list
    )
)

Creates an list of values by running each element of list thru iteratee. The iteratee is invoked with three arguments: (value, index, list).

square(n, index, list) {
  return n*n;
}
map([4, 8], square);
// Returns [16, 64]

Implementation

List map(List list, Function(dynamic element, int index, List list) iteratee) {
  int index = -1;
  int length = list.length;
  List result = List.generate(length, (i) => i);

  while (++index < length) {
    result[index] = iteratee(list[index], index, list);
  }

  return result;
}