@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).
Here we go with the explanations:
(function(x, f = () => x) {| /** | |
| * Cooperative tasks scheduler. | |
| * | |
| * by Dmitry Soshnikov <[email protected]> | |
| * MIT Style License | |
| * | |
| * An educational implementation to show the work of a processes scheduler. | |
| * In practice you may find using a library like Task.js, or similar. | |
| * | |
| * Test, Node 4 or transformed with Babel: |
| /** | |
| * Depth-first and Breadth-first graph traversals. | |
| * | |
| * In this diff we implement non-recursive algorithms for DFS, | |
| * and BFS maintaining an explicit stack and a queue. | |
| * | |
| * by Dmitry Soshnikov <[email protected]> | |
| * MIT Style license | |
| */ |
@kangax created a new interesting quiz, this time devoted to ES6 (aka ES2015). I found this quiz very interesting and quite hard (made myself 3 mistakes on first pass).
Here we go with the explanations:
(function(x, f = () => x) {| /** | |
| * LR-parsing. | |
| * | |
| * Canonical collection of LR(0) items. | |
| * | |
| * by Dmitry Soshnikov <dmitry.soshnikov@gmail.com> | |
| * MIT Style License (C) 2015 | |
| * | |
| * See this "rock-painting" to get the picture of what we're building here: | |
| * |
A full parsing table is not needed, only the canonical collection. In the canonical collection, find all final items (and only final items), and see if:
If none of these is true, there are no conflicts, even in LR(0). If there are some of the above, SLR(1) still may solve it.
| { | |
| "lex": { | |
| "macros": { | |
| "digit": "[0-9]", | |
| "esc": "\\\\", | |
| "int": "-?(?:[0-9]|[1-9][0-9]+)", | |
| "exp": "(?:[eE][-+]?[0-9]+)", | |
| "frac": "(?:\\.[0-9]+)" | |
| }, | |
| "rules": [ |
| %token STRING NULL NUMBER TRUE FALSE | |
| %start JSONText | |
| %% | |
| JSONText : JSONValue; | |
| JSONString : STRING; |
| /** | |
| * Destructuring reference bindings. | |
| * | |
| * by Dmitry Soshnikov <[email protected]> | |
| * MIT Style License | |
| */ | |
| // Bindings of a pattern in destructuring may be not only | |
| // simple variables, but any reference types. In the following | |
| // example we use destructuring assignment to swap two elements |
| ### | |
| # Explicit delegation-based prototypes implementation. | |
| # | |
| # Implementing JavaScript semantics. | |
| # | |
| # Here we show that "delegation" is just a mechanism (a pattern) for | |
| # implementation of the "inheritance" concept. In this specific case we | |
| # have direct inheritance from objects, skipping the "class" step. | |
| # | |
| # See "OO Relationships" article for some details: |
| /** | |
| * "Is connected?" | |
| * | |
| * Dynamic connectivity issue. | |
| * | |
| * 1 2: not connected | |
| * 1--2: connected | |
| * 1--2--3: 1 and 3 are connected through 2 | |
| * | |
| * This implementation uses concept of a "weight" to avoid too long trees: |