Skip to content

Instantly share code, notes, and snippets.

@domenic domenic/difference.md
Last active Jan 16, 2019

Embed
What would you like to do?
Set.prototype.difference, simplified

Context: https://github.com/tc39/proposal-set-methods/issues/50

Proposed spec texts

Set.prototype.difference(arg), minimalist

  1. If Type(this) is not Object, or this does not have a [[SetData]] internal slot, throw a new TypeError exception.
  2. If Type(arg) is not Object, or arg does not have a [[SetData]] internal slot, throw a new TypeError exception.
  3. Let diffList be a List containing all elements that are in this.[[SetData]] but are not in arg.[[SetData]], using SameValueZero to determine identity. (Optional: formalize this with a loop that operates on the Lists.)
  4. Let newSet be ? OrdinaryCreateFromConstructor(%Set%, "%SetPrototype%", « [[SetData]] »).
  5. Set newSet.[[SetData]] to diffList.
  6. Return newSet.

If we wanted to accept iterables as arguments, then we would do the following:

Set.prototype.difference(arg), supports iterables

  1. If Type(this) is not Object, or this does not have a [[SetData]] internal slot, throw a new TypeError exception.
  2. Let argList be the empty list.
  3. If arg is not undefined or null, then set argList to ? IterableToList(arg).
  4. Let diffList be a List containing all elements that are in this.[[SetData]] but are not in argList, using SameValueZero to determine identity. (Optional: formalize this with a loop that operates on the Lists.)
  5. Let newSet be ? OrdinaryCreateFromConstructor(%Set%, "%SetPrototype%", « [[SetData]] »).
  6. Set newSet.[[SetData]] to diffList.
  7. Return newSet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.