/* Copyright 2018 Nick Moore Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * Better Fill - 2/25/2018 - Nick Moore * Illustrates how to use generator functions to initialize and manipulate arrays. * * @@NOTE@@ * Generator functions are not natively supported by Internet Explorer (but are supported in Microsoft Edge). * In order to use these methods in IE, you will need to transpile your code using * a transpiler like Babel (https://babeljs.io). * For current browser support visit the link below * https://caniuse.com/#feat=es6-generators * * Reasoning: * JavaScript's `Array.prototype.fill` method is inadequate for initializing arrays. * I wanted to create a function that, in a single line of code, could give me an * array of `n` values. I also wanted to be able to give this new fill method a * function describing how each new value is constructed. * * Resources: * MDN Iterators and Generators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators * MDN Generator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator * MDN function*: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* * MDN Spread syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax */ /** * Generates `n` number of values * * @param n {Integer} * The number of values to generate. * @param valueOrFunction {any|(index: number) => any} * The value to return. If a function is passed, it will be called to get the value. * * @returns {Iterator} An iterator that can be used to obtain the values. */ function* generateValues(n, valueOrFunction) { const isFunc = typeof valueOrFunction === 'function'; for (let i = 0; i < n; i++) { yield isFunc ? valueOrFunction(i) : valueOrFunction; } } // This method uses the spread syntax for array literals. // It generates a new array containing 100 zeros. const a1 = [...generateValues(100, 0)]; console.log(a1); class Point { constructor(x, y) { this.x = x; this.y = y; } } // You can also give `generateValues` a function as the second argument. // The function receives the current index as an argument. const a2 = [...generateValues(100, i => new Point(i, i))] console.log(a2); // You can also use the spread syntax for function arguments // This calls push with 100 arguments and fills the array with ones. const a3 = []; a3.push(...generateValues(100, 1)); console.log(a3); // Iterables can be used in for..of loops. // This fills the array with 100 twos. const a4 = []; for(let val of generateValues(100, 2)) { a4.push(val); } console.log(a4); // This snippet below goes a little more in depth on how the iterables work in the for..of loop. // Note: Behind the scenes in the for..of loop above, `next` is called for you, `done` is checked, // and `value` is automatically assigned to the `val` variable. // See the MDN documentation for more info: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators const a5 = []; const g = generateValues(100, 3); let itr = g.next(); while(!itr.done) { a5.push(itr.value); itr = g.next(); } console.log(a5);