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
promise = v => (new Promise(r => setTimeout(x => r(v), ((Math.random() * (10 - 1)) + 1 | 0) * 1000))); | |
//execs all promises in parallel but prints numbers randomly | |
[promise(1),promise(2),promise(3),promise(4),promise(5),promise(6)].forEach(function(p) { | |
return p.then(v => console.log(v)); | |
}, Promise.resolve()); | |
//still execs promises in parallel and uses a sequence to print numbers in order | |
[promise(1),promise(2),promise(3),promise(4),promise(5),promise(6)].reduce(function(sequence, p) { |
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
// serialize asynchronous code | |
// Start off with a promise that always resolves | |
var sequence = Promise.resolve(); | |
// Loop | |
[1,2,3,4,5,6].forEach(function(v) { | |
// Add these actions to the end of the sequence | |
sequence = sequence.then(function() { | |
return new Promise(r => setTimeout(x => r(v), ((Math.random() * (10 - 1)) + 1 | 0) * 1000)); | |
}).then(v => console.log(v)); |
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 iterate(...args) { | |
let index = 0; | |
return { | |
[Symbol.iterator]() { | |
return this; | |
}, | |
next() { | |
if (index < args.length) { | |
return { value: args[index++] }; |
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 g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'}; | |
var g2 = function* (){ x = yield* g1(); yield 4; return 'bye' } | |
// yield: 1,2,3,4 ( x becomes 'hola'), bye, done | |
//else | |
var g1 = function* (){ yield 1; yield 2; yield 3; return 'hola'}; | |
var g2 = function* (){ yield* g1() }; |
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
p1 = function(r) { | |
console.log(new Date(), r); | |
p2 = new Promise(r => setTimeout(x => r(3), 3000)); | |
p3 = new Promise(r => setTimeout(x => r(4), 7000)); | |
p4 = new Promise(r => setTimeout(x => r(5), 2000)); | |
s = Promise.resolve(); | |
s2 = s.then(x => p2); |
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
/* | |
Pipes two functions | |
*/ | |
function pipe(fn, fnPipe, obj){ | |
var fnx = function(){}; | |
if (typeof fn === 'function'){ | |
fnx = function(){ | |
var res = fn.apply(this, [].slice.apply(arguments)); | |
return typeof fnPipe === 'function' ? fnPipe.call(this, res) : res; | |
}.bind(obj || window); |
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 attempt(obj, p, fnFail){ | |
var fn = function(){}; | |
if (obj && typeof obj[p] === 'function'){ | |
fn = function(){ | |
try { | |
return this[p].apply(this, [].slice.apply(arguments)); | |
}catch(e){ | |
return typeof fnFail === 'function' ? fnFail(e, p, obj) : console.error('attempt fail:', this['__id__'] || '', p, e); | |
} | |
}.bind(obj); |
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
/* | |
wraps the given function p in a try catch | |
so is safer to call it in some contexts. e.g. analytic functions in unit tests. | |
*/ | |
Object.prototype.try = function (p){ | |
var fn = function(){}; | |
if (typeof this[p] === 'function'){ | |
fn = function(){ | |
try { | |
this[p].apply(this, [].slice(arguments)); |
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 toArray = Function.prototype.call.bind(Array.prototype.slice); |
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
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
var input = ""; | |
process.stdin.on("data", function (chunk) { | |
input += chunk; | |
}); | |
process.stdin.on("end", function () { | |
// now we can read/parse input | |
var n = parseInt(input, 10); | |
var str = []; |