Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use destructuring assignment to define enumerations in ES6?

You can use destructuring assignment to define enumerations in ES6 as follows:

var [red, green, blue] = [0, 1, 2];

Instead, I'd like the right hand side of the destructuring assignment to be dynamic. For example:

var MAX_ENUM_SIZE = 32;
var ENUM = new Array(MAX_ENUM_SIZE);
for (var i = 0; i < MAX_ENUM_SIZE; i++) ENUM[i] = i;

var [red, green, blue] = ENUM;

Unfortunately, this seems like a hack. What if I want a bigger enumeration in the future? Hence, I was thinking of using destructuring assignment with an iterator as follows:

var [red, green, blue] = enumeration(/* I don't want to specify size */);

However, I don't think it's possible to use destructuring assignment with iterators[citation needed]. Is there any way to accomplish this goal?

like image 362
Aadit M Shah Avatar asked Sep 24 '16 07:09

Aadit M Shah


1 Answers

Use a generator

function* enumerator() {
  let i = 0;
  while (true) yield i++;
};

let [red,green,blue] = enumerator();
console.log(red, green, blue); // 0 1 2

let [a,b,c,d,e] = enumerator();
console.log(a,b,c,d,e); // 0 1 2 3 4

The generator is flexible making this pretty neat for implementing different types of enums – for example, these cute bitmask enums

function* bitmask() {
  let i = 0;
  while (i < 32) yield 1 << i++;
  throw Error("bitmask enumerator exceeds 32 bits");
}

let [R,W,X] = bitmask();

const read = p => (p & R) !== 0;
const write = p => (p & W) !== 0;
const exec = p => (p & X) !== 0;

{
  let p = R | W; // read and write only
  console.log("can read?", read(p));   // true
  console.log("can write?", write(p)); // true
  console.log("can exec?", exec(p));   // false
}

{
  let p = R | X; // read and execute only
  console.log("can read?", read(p));    // true
  console.log("can write?", write(p));  // false
  console.log("can exec?", exec(p));    // true
}
like image 99
Mulan Avatar answered Sep 19 '22 15:09

Mulan