Implement map()...with bugs EDIT: The behavior may be inconsistent. Also, associative arrays are permitted.

Make a function that iterates through an indexed array, mapping a function of a single argument to each member of the array. The catch: introduce a hidden bug where the function is not correctly applied to at least one index of the input array.

Example:



map(lambda a: a+1, [1, 2, 3, 4])
# Should be [2, 3, 4, 5]
# Could be: [2, 3, 5, 4] or [2, 4, 6, 5] or anything else


Rules:


Use either a static method with two arguments, function and array, or a non-static method with one, an array or function (this being of the other type). Examples:


std::vector&lt;F&gt; map(F (*fn)(E), std::vector&lt;E&gt; array); (C++)
def map(fn, array): ... (Python)
Array.prototype.map = function (fn) {...}; (JavaScript)

If your language features a method already to do this, re-implement it.
The ignored entry may be of any type desired.
Assume the arguments to actually be correct (if the language has dynamic type checking).
It may or may not be in place.
Hide your malice.
Highest vote count wins.


Here's an example of a correct, bug-free implementation in JavaScript (excluding type checking):

Array.prototype.map = function (fn) {
  var i   = this.length,
  ret = new Array(i);
  while (i--) { // decrements to 0 after evaluation
    ret[i] = fn(this[i]);
  }
  return ret;
};
Perl

sub mymap (&amp;@) {
  my $fn = shift;
  my @ret;
  while (my $val = shift) {
    local $_ = $_[0]; # map calling convention is element in $_
    push @ret, $fn-&gt;();
  }
  return @ret;
}


Looks plausible for at least a moment, until you have the sense to go "why isn't he using $val?"