Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I don't want to mutate an array in order to implement map

Er, what?

  > foo = [1, 2, 3]
  [1, 2, 3]
  > foo.map(function(i) { return i + 1; })
  [2, 3, 4]
  > foo
  [1, 2, 3]


That is using map, not implementing map.

Implementing map:

   function map(f, ary) {
      var ret = [];
      for(var i = 0; i < ary.length; i+= 1) {
         ret.push(f(ary[i]));
      }
      return ret;
   }
Though that is not a good example by the op, because that is what you do:

https://github.com/clojure/clojure/blob/master/src/clj/cloju...


I would do it like this...

    function map(f, ary) {
        return [].map.call(ary, f);
    }


That's _using_ map. Read an implementation of map to see what I mean.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: