set.empty() method

# Peter Michaux (12 years ago)

If some piece of code needs to empty a set, it would be good to do that in a single call

set.empty();

Otherwise we might be left doing the following which could be very inefficient.

set.forEach(function(element) {
    set['delete'](element);
});

Peter

# Brendan Eich (12 years ago)

Good idea, but I suggest an unambiguous verb instead of an adjective-or-verb: clear.

"empty" is often used for the predicate, in naming conventions that eschew "isEmpty" and "emptyp" patterns.

# Adam Shannon (12 years ago)

I'd agree with using clear() and isEmpty() with their respective actions.

# Mark S. Miller (12 years ago)

clear() is ok. Also, java.util.Map and java.util.Set use clear() so it would also be familiar to many people.

Perhaps deleteAll() would be more mnemonic, as its relationship with delete() would be obvious?

# Adam Shannon (12 years ago)

deleteAll() seems confusing because (even though sets aren't designed like this) it seems like deleteAll(key) would delete all entries that are mapped from key.

# Andrea Giammarchi (12 years ago)

set.values().forEach(set.delete, set);

+1 on clear(), a shortcut would be nice

# Andreas Rossberg (12 years ago)

On 15 February 2012 07:47, Mark S. Miller <erights at google.com> wrote:

clear() is ok. Also, java.util.Map and java.util.Set use clear() so it would also be familiar to many people.

Perhaps deleteAll() would be more mnemonic, as its relationship with delete() would be obvious?

+1 for deleteAll. Give related names to related operations.

# Michael A. Smith (12 years ago)

+1 on clear() as it's pithy, understandable, and behaves the same in Java and Python.

What real benefit comes from drawing this connection between delete() and deleteAll()? I suspect the everyday programmer won't care, and the ones who do will look it up.

-Michael A. Smith

# Kevin Smith (12 years ago)

Not to backtrack the conversation, but I'm not convinced that "delete" should be used to remove an element. Noone's going to ask me, "how do I delete an element from the set?". They're going to ask me, "how do I remove an element?". Also, I'm currently suspicious of any parallels between deleting a property and removing an element from a set. The whole point of Set and Map is to provide a collection abstraction independent of javascript's object model (so as to end the confusion that arises from their conflation), correct?

Plus, syntax-ignorant tokenizers (like the highlighter on the proposal page) are going to make delete look pretty weird : )

khs

# Dean Landolt (12 years ago)

On Wed, Feb 15, 2012 at 8:58 AM, Kevin Smith <khs4473 at gmail.com> wrote:

Not to backtrack the conversation, but I'm not convinced that "delete" should be used to remove an element. Noone's going to ask me, "how do I delete an element from the set?". They're going to ask me, "how do I remove an element?".

That's a good point. Just like noone's going to ask "how do I put an element".

add : remove :: put : delete

Also, I'm currently suspicious of any parallels between deleting a property and removing an element from a set. The whole point of Set and Map is to provide a collection abstraction independent of javascript's object model (so as to end the confusion that arises from their conflation), correct?

This is a good argument in favor of "clear" as well. You never really want to "clear" a typical object, it only really makes sense in the context of collections.

# John Tamplin (12 years ago)

On Wed, Feb 15, 2012 at 4:55 AM, Andreas Rossberg <rossberg at google.com>wrote:

On 15 February 2012 07:47, Mark S. Miller <erights at google.com> wrote:

clear() is ok. Also, java.util.Map and java.util.Set use clear() so it would also be familiar to many people.

Perhaps deleteAll() would be more mnemonic, as its relationship with delete() would be obvious?

+1 for deleteAll. Give related names to related operations.

deleteAll would seem better reserved for deleteAll(Collection), which would remove every element present in the collection.

# Brendan Eich (12 years ago)

I agree and find the add/remove pairing to be winning compared to add/delete. It dodges the ES3 issue. I'd go for pithy over verb-noun-y any day, so clear > deleteAll. Please, y'all, save us from Java-esque

logorrhea!