4 ms·
> 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]
by saraid216 13y ago
> 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]
- davexunit 13y agoThat's _using_ map. Read an implementation of map to see what I mean.
- i_s 13y agoThat 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/clojure/core.clj#L2556-#L2557 https://github.com/clojure/clojure/blob/master/src/clj/cloju...
- deleted 13y ago[deleted]
- gotofritz 13y agoI would do it like this... function map(f, ary) { return [].map.call(ary, f); }