This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a Proxy for the lazy construction of an object | |
function buildLazyObject(constructorFn, ...args){ | |
let internalObject = null; | |
//we don't care about the target, but compiler does not allow a null one, so let's pass an "empty object" {} | |
return new Proxy({}, { | |
get: function(target, property, receiver){ | |
console.log("this === receiver " + (this === receiver)); //false | |
//"this" is not the proxy, but the handler object containing the traps (obviously are not the same object) | |
internalObject = internalObject || (() => { | |
console.log("Creating object"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//we don't need to inherit from Promise, await works fine with just a "thenable" | |
//notice in the second part of the code that Promise.resolve of a thenable returns a new Promise rather than the thenable | |
class LazyPromise { | |
constructor(creationFn){ | |
console.log("creating LazyPromise"); | |
this.initialized = false; | |
this.creationFn = creationFn; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transformString (source){ | |
if (source.length === 0) | |
return ""; | |
if (source.length === 1) | |
return source.toUpperCase() + source; | |
let curItem = source[0]; | |
let transformed = transformString(source.substring(1)); | |
return curItem.toUpperCase() + transformed + curItem; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const smallValue = 100; | |
const largeValue = 50000; | |
function factorial(n) { | |
return (n !== 0) ? n * factorial(n - 1) : 1; | |
} | |
function tailFactorial(n, total = 1) { | |
//console.trace(); | |
if (n === 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const printDebug = (msg) => console.log(msg); | |
class AsyncExtendedIterable{ | |
constructor(iterable){ | |
this.iterable = iterable; | |
} | |
async *[Symbol.asyncIterator](){ | |
for await (let it of this.iterable){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let calculator = { | |
simpleCalculation(n){ | |
return 25 * n; | |
}, | |
complexCalculation(n){ | |
return new Promise(res => { | |
setTimeout(() => res(44 * n), 3000) | |
}); | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Factory Function to combine multiple sort criteria | |
// receives functions representing sorting criteria | |
// returns a new function that applies these criteria | |
function sortByMultipleCriteriaFactory(...sortFuncs){ | |
return (it1, it2) => { | |
for (sort of sortFuncs){ | |
let res = sort(it1, it2); | |
if (res !== 0){ | |
//for this criteria items are not equal (in sorting terms) so no more criteria to apply | |
return res; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//I'm calling "task" to a function that when executed returns a Promise | |
class PromiseAllWithLimit{ | |
constructor(tasks, maxItems){ | |
this.allTasks = tasks; | |
this.maxItems = maxItems; | |
this.taskResults = []; | |
this.nextTaskId = 0; | |
this.resolveAll = null; //points to the resolve function to invoke when all tasks are complete | |
this.completedCounter = 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExtendedIterable{ | |
constructor(iterable){ | |
this.iterable = iterable; | |
} | |
*[Symbol.iterator](){ | |
for (let it of this.iterable){ | |
yield it; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
class Formatter{ | |
constructor(repetitions){ | |
this.repetitions = repetitions; | |
} | |
format1(txt){ | |
return `${"[" .repeat(this.repetitions)} ${txt} ${"]" .repeat(this.repetitions)}`; | |
} |