Skip to content

Instantly share code, notes, and snippets.

@abstractmachines
Last active July 5, 2018 19:17

Revisions

  1. abstractmachines revised this gist Jul 5, 2018. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions set-map-iterator.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,21 @@
    /* JavaScript Iterators
    Sets and Maps
    Sets and Maps can be made Iterable/Iterator by using the methods .entries(), .keys(), values(), and/or via for of construct.
    Sets and Maps can be made Iterable/Iterator by using the methods .entries(), .keys(),
    values(), and/or via for of construct.
    A Set is a unique (non repeated) version of a Bag data structure. Characteristic equation is contains() or has().
    A Set is a unique (non repeated) version of a Bag data structure. Characteristic equation
    is contains() or has().
    A Map (in JS) is a key-value pair data structure, unordered (in order of insertion).
    In ES6 (and beyond) we tend to use Maps instead of Objects for iteration, because the Object prototype chain can be
    problematic for iteration.
    Map keys can be any data type (objects, functions, primitives); Object keys can only be Strings/Symbols.
    Maps also have better performance for frequent insertions and deletions according to MDN,
    but the MDN article does not specify whether Maps are more performant temporally or for memory.
    In ES6 (and beyond) we tend to use Maps instead of Objects for iteration, because the Object
    prototype chain can be problematic for iteration.
    Map keys can be any data type (objects, functions, primitives); Object keys can only be
    Strings/Symbols. Maps also have better performance for frequent insertions and deletions
    according to MDN, but the MDN article does not specify whether Maps are more performant
    temporally or for memory.
    Sources:
    1 - Data structures and algorithms at Princeton and MIT open courseware
    @@ -29,7 +33,7 @@ const jazzCats = new Set( [ 'paul chambers', 'monk', 'coltrane' ] )
    > jazzCats
    Set { 'paul chambers', 'monk', 'coltrane' }

    // Sets.entries() makes iterable. Obviously .keys() and .values() would produce identical results to this.
    // Sets.entries() makes iterable. Obviously .keys() and .values() would produce identical results.
    > jazzCats.entries()
    SetIterator {
    [ 'paul chambers', 'paul chambers' ],
  2. abstractmachines created this gist Nov 2, 2017.
    54 changes: 54 additions & 0 deletions set-map-iterator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    /* JavaScript Iterators
    Sets and Maps
    Sets and Maps can be made Iterable/Iterator by using the methods .entries(), .keys(), values(), and/or via for of construct.
    A Set is a unique (non repeated) version of a Bag data structure. Characteristic equation is contains() or has().
    A Map (in JS) is a key-value pair data structure, unordered (in order of insertion).
    In ES6 (and beyond) we tend to use Maps instead of Objects for iteration, because the Object prototype chain can be
    problematic for iteration.
    Map keys can be any data type (objects, functions, primitives); Object keys can only be Strings/Symbols.
    Maps also have better performance for frequent insertions and deletions according to MDN,
    but the MDN article does not specify whether Maps are more performant temporally or for memory.
    Sources:
    1 - Data structures and algorithms at Princeton and MIT open courseware
    2 - Axel Rauschmeyer: http://exploringjs.com/es6/ch_iteration.html
    3 - http://www.zsoltnagy.eu/es6-iterators-and-generators-in-practice/
    4 - MDN on Maps: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
    */

    // Code done on node REPL instead of WebpackBin

    // Set

    const jazzCats = new Set( [ 'paul chambers', 'monk', 'coltrane' ] )

    > jazzCats
    Set { 'paul chambers', 'monk', 'coltrane' }

    // Sets.entries() makes iterable. Obviously .keys() and .values() would produce identical results to this.
    > jazzCats.entries()
    SetIterator {
    [ 'paul chambers', 'paul chambers' ],
    [ 'monk', 'monk' ],
    [ 'coltrane', 'coltrane' ] }

    // Map

    const artists = new Map( [[ 'jazz', 'coltrane'], ['funk', 'curtis mayfield' ]] )

    > artists
    Map { 'jazz' => 'coltrane', 'funk' => 'curtis mayfield' }

    > artists.entries()
    MapIterator { [ 'jazz', 'coltrane' ], [ 'funk', 'curtis mayfield' ] }

    > artists.keys()
    MapIterator { 'jazz', 'funk' }

    > artists.values()
    MapIterator { 'coltrane', 'curtis mayfield' }