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 myButton = document.getElementById("myButton"); | |
| fromEvent(myButton, "click") | |
| |> reduce(#, acc => acc + 1, 0) | |
| |> forEach(#, count => console.log(count)); |
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 fromEvent(element, event, options) { | |
| return fromCallback(listener => { | |
| element.addEventListener(event, listener, options); | |
| }); | |
| } |
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 fromCallback(emitter) { | |
| let values; | |
| let resolve; | |
| const init = r => [values, resolve] = [[], r]; | |
| let valuesAvailable = new Promise(init); | |
| emitter(value => { | |
| values.push(value); | |
| resolve(values); | |
| }); | |
| return { |
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* filter(src, op) { | |
| let index = 0; | |
| for await (const value of src) { | |
| if (op(value, index++)) { | |
| yield value; | |
| } | |
| } | |
| } | |
| async function* map(src, op) { |
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* map(src, op) { | |
| let index = 0; | |
| for (const value of src) { | |
| yield op(value, index++); | |
| } | |
| } | |
| function* filter(src, op) { | |
| let index = 0; | |
| for (const value of src) { |
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
| timedGen() |> filter(#, v => v % 2 === 0) |> forEach(#, 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
| const wait = delay => new Promise(resolve => setTimeout(resolve, delay)); | |
| async function* timedGen(count = 10) { | |
| for (let i = 0; i < count; i++) { | |
| await wait(1000); | |
| yield i; | |
| } | |
| } | |
| (async function () { |
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 wait = (value, delay = 1000) => new Promise(resolve => setTimeout(() => resolve(value), delay)); | |
| const timedSequence = { | |
| [Symbol.asyncIterator]() { | |
| let count = 0; | |
| return { | |
| next() { | |
| return wait({ | |
| value: count++, | |
| done: count > 10 |
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
| for await (const event of sequence) { | |
| console.log(event); | |
| } |
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
| forEach(collection, item => console.log(item)); |