Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please add .pop() #1

Open
Ginden opened this issue Jan 27, 2018 · 6 comments
Open

Please add .pop() #1

Ginden opened this issue Jan 27, 2018 · 6 comments

Comments

@Ginden
Copy link
Collaborator

Ginden commented Jan 27, 2018

From @tabatkins on January 23, 2018 22:16

Popping from a Set/Map is actually very useful, regardless of whether there's a direction/ordering or not. The semantics are just "remove one arbitrary value from the set and give it to me", which is annoyingly clumsy to implement by hand.

I've used .pop() in Python several times, and felt the lack of it in JS projects.

Copied from original issue: tc39/proposal-set-methods#18

@ljharb
Copy link
Member

ljharb commented Jan 17, 2019

@tabatkins Would this need to pull the last one? could it pull the first?

@tabatkins
Copy link

Don't care; authors should be treating Sets as unordered, anyway, and accept the orderedness as an accidental implementation detail we're forced to bake in. ^_^

@zloirock
Copy link
Contributor

zloirock commented Jan 17, 2019

For me makes sense an equal of .shift - getting and removing the first element from entries chain. JS Set at this moment is ordered collection, adding a getter of the last element of the collection with O(1) complexity is adding a new fundamental conception to JS collections, not just sugar for available at this moment features and looks redundant.

@ljharb
Copy link
Member

ljharb commented Jan 17, 2019

So, some kind of sugar like:

unshift() {
  for (const item of this) {
    this.delete(item);
    return item;
  }
}

?

@zloirock
Copy link
Contributor

zloirock commented Jan 17, 2019

Yep. One moment - I made a typo, I mean .shift.

@tabatkins
Copy link

Whether we name the function unshift() or pop(), we should probably match the semantic of the same function on Arrays, and return either the first or last accordingly.

So yeah, if we want to return the first item, in accordance with that simple desugaring, the function should be called unshift() I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants