Skip to content

Instantly share code, notes, and snippets.

@domenic
Last active January 16, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenic/44773a7610dc8541bf2a83df2a3ce990 to your computer and use it in GitHub Desktop.
Save domenic/44773a7610dc8541bf2a83df2a3ce990 to your computer and use it in GitHub Desktop.
Set.prototype.difference, simplified

Context: tc39/proposal-set-methods#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