Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / process-scheduler.js
Created October 15, 2015 19:58
Educational cooperative processes scheduler
/**
* 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:
@DmitrySoshnikov
DmitrySoshnikov / dfs-bfs-non-recursive.js
Created October 19, 2015 05:40
Non-recursive DFS and BFS algorithms
/**
* 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's ES6 quiz, explained

@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:

Question 1:
(function(x, f = () =&gt; x) {
@DmitrySoshnikov
DmitrySoshnikov / lr0-items.js
Last active February 15, 2023 14:56
LR Parsing: Canonical collection of LR(0) items
/**
* LR-parsing.
*
* Canonical collection of LR(0) items.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License (C) 2015
*
* See this "rock-painting" to get the picture of what we're building here:
*
@DmitrySoshnikov
DmitrySoshnikov / sr-rr-confilct.md
Last active May 13, 2024 13:09
Parsing notes: "Shift-reduce" and "Reduce-reduce" conflicts in LR parsing

"Shift-reduce" and "Reduce-reduce" conflicts in LR parsing.

How to determine?

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:

  • There are both shift and reduce in the same item ("shift-reduce", s/r)
  • There are two reduce actions in the same item ("reduce-reduce", r/r)

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": [
@DmitrySoshnikov
DmitrySoshnikov / json.bnf
Created January 10, 2016 04:40
json.bnf
%token STRING NULL NUMBER TRUE FALSE
%start JSONText
%%
JSONText : JSONValue;
JSONString : STRING;
@DmitrySoshnikov
DmitrySoshnikov / es2015-destructuring-reference-bindings.js
Last active January 24, 2016 05:32
ES2015: Destructuring reference bindings
/**
* 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
@DmitrySoshnikov
DmitrySoshnikov / delegation-based-prototypes.py
Last active January 30, 2016 09:23
Delegation-based prototypes: implementing JavaScript semantics in Python
###
# 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:
@DmitrySoshnikov
DmitrySoshnikov / union-quick-union-weight.js
Created February 4, 2016 05:42
Union.QuickUnion.Weight
/**
* "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: