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 delay = (ms, result) => | |
new Promise(resolve => setTimeout(() => resolve(result), ms)); |
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
async function delays() { | |
let a = await delay(800, "Hello, I'm in an"); | |
console.log(a); | |
let b = await delay(400, "async function!"); | |
console.log(b); | |
} | |
delays(); |
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 delays() { | |
return delay(800, "Hello, I'm in a").then(a => { | |
console.log(a); | |
return delay(400, "Promise!"); | |
}).then(b => { | |
console.log(b); | |
}); | |
} | |
// call 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
function* delays() { | |
let a = yield delay(800, "Hello, I'm an"); | |
console.log(a); | |
let b = yield delay(400, "async coroutine!"); | |
console.log(b); | |
} | |
// call it | |
coroutine()(delays()); |
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* delays() { | |
let a = yield delay(800, "Hello, I'm an"); | |
console.log(a); | |
let b = yield delay(400, "async coroutine!"); | |
console.log(b); | |
} | |
const coroutine = nextValue => iterator => { | |
const { done, value } = iterator.next(nextValue); |
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* naturals() { | |
let i = 1; | |
while (true) { | |
// it can count to infinity | |
yield i++; | |
} | |
} | |
for (let i of naturals()) { | |
// count only to 5 |
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 AlternatingArray extends Array { | |
constructor(...args) { | |
super(...args); | |
} | |
*[Symbol.iterator](){ | |
for (let i = 0; i < this.length; i += 2) { | |
yield this[i]; | |
} | |
for (let i = 1; i < this.length; i += 2) { | |
yield this[i]; |
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
// merge two arrays a and b | |
const merge = (a, b) => a.reduce( | |
(acc, curr, i) => [ | |
...acc, // copy all previous elements | |
curr, // current element of a | |
b[i] // current element of b | |
], | |
[] // initial value | |
); |
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
curry = f => a => b => f(a, b) | |
uncurry = f => (a, b) => f(a)(b) | |
papply = (f, a) => b => f(a, b) |
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
add = (a, b) => a + b | |
curriedAdd = a => b => a + b | |
add(5,6) === 11 | |
curriedAdd(5)(6) === 11 | |
uncurry(curry(add))(5,6) === 11 | |
curry(uncurry(curriedAdd))(5)(6) === 11 |