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 even(n) { | |
| if (n === 0) return true; | |
| else return odd(n-1); | |
| } | |
| function odd(n) { | |
| if (n === 0) return false; | |
| else return even(n-1); | |
| } | |
| console.log(even(100000)); |
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 FAIL = {}; | |
| let _pos; | |
| let _input; | |
| function parse(input) { | |
| _input = input; | |
| _pos = 0; | |
| const r = parens(); | |
| if (r === FAIL || _pos !== _input.length) throw new Error('parse error'); | |
| return r; |
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
| // parser = string -> [match, remainder] | |
| // read single item | |
| function* item(s) { | |
| if (s.length > 0) yield [s[0], s.slice(1)]; | |
| } | |
| function run(p, s) { | |
| return [...p(s)] | |
| } |
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 letters = c => ({ | |
| 0: " ", | |
| 1: "", | |
| 2: "abc", | |
| 3: "def", | |
| 4: "ghi", | |
| 5: "jkl", | |
| 6: "mno", | |
| 7: "pqrs", | |
| 8: "tuv", |
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 flatMap = (f, as) => { | |
| const result = []; | |
| for (const a of as) { | |
| result.push(...f(a)); | |
| } | |
| return result; | |
| } | |
| const map = (f, as) => { | |
| const result = []; |
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 traverse = (f, as) => sequence(map(f, as)); | |
| const sequence = (as) => { | |
| if (as.length === 0) return [""]; | |
| return flatMap(a => map(b => [a, ...b], sequence(as.slice(1))), as[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
| var letterCombinations = function(digits) { | |
| if (digits.length === 0) return []; | |
| const result = ['']; | |
| const letters = ' ||abc|def|ghi|jkl|mno|pqrs|tuv|wxyz'.split('|'); | |
| for (const d of digits) { | |
| for (const r of result.splice(0)) { | |
| for (const l of letters[+d]) { | |
| result.push(r + l); | |
| } |
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 createAsyncIterator() { | |
| const self = this; | |
| return ag(function*() { | |
| const resume = []; | |
| const values = []; | |
| const subscription = self.materialize().subscribe(value => { | |
| values.push(value); | |
| resume.splice(0).forEach(r => r()); | |
| }); |
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
| export function ag(f) { | |
| const promises = []; | |
| const g = f(); | |
| return { | |
| [Symbol.asyncIterator]() { | |
| return this; | |
| }, | |
| next(input) { | |
| if (promises.length > 0) { | |
| return new Promise((resolve, reject) => { |
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* createAsyncIterator() { | |
| const promise = []; | |
| const values = []; | |
| const subscription = this.materialize().subscribe(value => { | |
| values.push(value); | |
| promise.splice(0).forEach(r => r()); | |
| }); | |
| try { |