[Map/Set] Add an .update() method, a la Python?

# Tab Atkins Jr. (12 years ago)

Has it been discussed yet to add an .update() method to Map and Set, a la Python, which take an iterable (yielding [key, value] for Map, and just value for Set) and add the iterable's entries to the Map/Set?

It desugars easily:

Map.prototype.update = function(iter) {
  Map(iter).forEach((v,k)=>this.set(k,v));
};
Set.prototype.update = function(iter) {
  Set(iter).forEach(v=>this.add(v));
};

I've used this kind of dict/set addition in Python plenty, and added the method to my Map polyfill that I use for some personal projects.

Thoughts?

# Allen Wirfs-Brock (12 years ago)

On Jun 27, 2013, at 4:47 PM, Tab Atkins Jr. wrote:

Has it been discussed yet to add an .update() method to Map and Set, a la Python, which take an iterable (yielding [key, value] for Map, and just value for Set) and add the iterable's entries to the Map/Set?

It desugars easily:

Map.prototype.update = function(iter) { Map(iter).forEach((v,k)=>this.set(k,v)); }; Set.prototype.update = function(iter) { Set(iter).forEach(v=>this.add(v)); };

I've used this kind of dict/set addition in Python plenty, and added the method to my Map polyfill that I use for some personal projects.

Thoughts?

for (let [k,v] of iter) myMap.set(k,v)

# Tab Atkins Jr. (12 years ago)

On Thu, Jun 27, 2013 at 5:06 PM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:

On Jun 27, 2013, at 4:47 PM, Tab Atkins Jr. wrote:

Has it been discussed yet to add an .update() method to Map and Set, a la Python, which take an iterable (yielding [key, value] for Map, and just value for Set) and add the iterable's entries to the Map/Set?

It desugars easily:

Map.prototype.update = function(iter) { Map(iter).forEach((v,k)=>this.set(k,v)); }; Set.prototype.update = function(iter) { Set(iter).forEach(v=>this.add(v)); };

I've used this kind of dict/set addition in Python plenty, and added the method to my Map polyfill that I use for some personal projects.

Thoughts?

for (let [k,v] of iter) myMap.set(k,v)

Right, it's easy to write yourself, but it's a common enough operation that Python found it worthwhile to add, and I found it useful for my own purposes.

In particular, I like that it just returns the modified map/set, so it's easy to chain with, or to immediately return.

It's basically equivalent to dest.splice(-1, 0, ...source) for Arrays. (Or concat, if you ignore the fact that it doesn't alter the destination.)

# Rick Waldron (12 years ago)

On Thu, Jun 27, 2013 at 8:11 PM, Tab Atkins Jr. <jackalmage at gmail.com>wrote:

On Thu, Jun 27, 2013 at 5:06 PM, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:

On Jun 27, 2013, at 4:47 PM, Tab Atkins Jr. wrote:

Has it been discussed yet to add an .update() method to Map and Set, a la Python, which take an iterable (yielding [key, value] for Map, and just value for Set) and add the iterable's entries to the Map/Set?

It desugars easily:

Map.prototype.update = function(iter) { Map(iter).forEach((v,k)=>this.set(k,v)); }; Set.prototype.update = function(iter) { Set(iter).forEach(v=>this.add(v)); };

I've used this kind of dict/set addition in Python plenty, and added the method to my Map polyfill that I use for some personal projects.

Thoughts?

for (let [k,v] of iter) myMap.set(k,v)

Right, it's easy to write yourself, but it's a common enough operation that Python found it worthwhile to add, and I found it useful for my own purposes.

My gut reaction was the same as Allen's, but Ruby's Hash also has update(other_hsh) (though it is the same as merge!)

In particular, I like that it just returns the modified map/set, so it's easy to chain with, or to immediately return.

I was wondering about this while reading your OP, but you're not returning anything in that "desugaring". Anyway, I agree with this reasoning.

# Tab Atkins Jr. (12 years ago)

On Thu, Jun 27, 2013 at 5:26 PM, Rick Waldron <waldron.rick at gmail.com> wrote:

On Thu, Jun 27, 2013 at 8:11 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:

In particular, I like that it just returns the modified map/set, so it's easy to chain with, or to immediately return.

I was wondering about this while reading your OP, but you're not returning anything in that "desugaring". Anyway, I agree with this reasoning.

Whoops, I left that out accidentally. Modified (also to use Allen's for-of rather than forEach):

Map.prototype.update = function(iter) { for([k,v] of iter) this.set(k,v); return this; }; Set.prototype.update = function(iter) { for(v of iter) this.add(v); return this; };

# Rick Waldron (12 years ago)

On Thu, Jun 27, 2013 at 8:30 PM, Tab Atkins Jr. <jackalmage at gmail.com>wrote:

Whoops, I left that out accidentally. Modified (also to use Allen's for-of rather than forEach):

I agree with this and I think these should be added to the pool of methods to consider for Map and Set. The "pool" I refer to includes, but is not limited to:

  • union
  • intersect
  • difference
  • update (as of now)
# Kris Kowal (12 years ago)

I've found it satisfyingly idiomatic to call this addEach (for both maps and other collections) in my work on Collections.

# Rick Waldron (12 years ago)

On Thu, Jun 27, 2013 at 8:37 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

On Thu, Jun 27, 2013 at 8:30 PM, Tab Atkins Jr. <jackalmage at gmail.com>wrote:

On Thu, Jun 27, 2013 at 5:26 PM, Rick Waldron <waldron.rick at gmail.com> wrote:

On Thu, Jun 27, 2013 at 8:11 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:

In particular, I like that it just returns the modified map/set, so it's easy to chain with, or to immediately return.

I was wondering about this while reading your OP, but you're not returning anything in that "desugaring". Anyway, I agree with this reasoning.

Whoops, I left that out accidentally. Modified (also to use Allen's for-of rather than forEach):

Map.prototype.update = function(iter) { for([k,v] of iter) this.set(k,v); return this; }; Set.prototype.update = function(iter) { for(v of iter) this.add(v); return this; };

I agree with this and I think these should be added to the pool of methods to consider for Map and Set. The "pool" I refer to includes, but is not limited to:

  • union
  • intersect
  • difference
  • update (as of now)

Sorry, instead of "for Map and Set", I should use the more generic "for iterables" when generalizing, since not all Set methods are relevant to Map objects and not all Map methods are relevant to Set objects.