Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / union-quick-union.js
Created November 12, 2014 00:02
Union.QuickUnion
/**
* "Is connected?"
*
* Dynamic connectivity issue.
*
* 1 2: not connected
* 1--2: connected
* 1--2--3: 1 and 3 are connected through 2
*
* This implements quick union operation, but the find
@DmitrySoshnikov
DmitrySoshnikov / union-find.js
Last active March 6, 2018 22:44
"Is connected?" (Union Find)
/**
* "Is connected?"
*
* Dynamic connectivity issue.
*
* 1 2: not connected
* 1--2: connected
* 1--2--3: 1 and 3 are connected through 2
*
* by Dmitry Soshnikov <[email protected]>
@DmitrySoshnikov
DmitrySoshnikov / math-expr-lexer.cpp
Created October 13, 2014 06:04
Simple math expression lexer.
// by Dmitry Soshnikov <[email protected]>
// MIT Style License.
// Short URL for this example to run online is: http://cpp.sh/333
#include <iostream>
#include <vector>
#include <ctype.h>
#include <cstring>
@DmitrySoshnikov
DmitrySoshnikov / Map.prototype.map.md
Last active January 3, 2022 15:41
Map.prototype.map

Map.prototype.map ( callbackfn [ , thisArg ] )

The map method calls MapTransform abstract operation passing this value as M, callbackfn, thisArg as is, and false as updateKey.

The length property of the map method is 1.

Map.prototype.mapEntries ( callbackfn [ , thisArg ] )

The mapEntries method calls MapTransform abstract operation passing this value as M, callbackfn, thisArg as is, and true as updateKey.

@DmitrySoshnikov
DmitrySoshnikov / Map.prototype.filter.md
Created October 1, 2014 22:24
Map.prototype.filter

Map.prototype.filter ( callbackfn [ , thisArg ] )

NOTE callbackfn should be a function that accepts three arguments. filter calls callbackfn once for each key/value pair present in the map object, in key insertion order, and returns a new filtered map. callbackfn is called only for keys of the map which actually exist; it is not called for keys that have been deleted from the map.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.

callbackfn is called with three arguments: the value of the item, the key of the item, and the Map object being traversed.

filter does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.

@DmitrySoshnikov
DmitrySoshnikov / pattern-matching.md
Last active August 29, 2015 14:05
Pattern Matching

Pattern Matching

In this article we briefly describe the generic topic of pattern matching in programming languages. We'll see that this powerful technique is present in our every day programming, even if we may not notice it.

Matching

A matching is a process of checking whether one thing has the same or similar representation as another thing.

There are two basic matches:

/**
* Apply a function in needed environment.
* Firefox only, educational purpose only.
* by Dmitry Soshnikov <[email protected]>
*/
Function.prototype.setEnvironment = function(environment) {
with (environment) {
return eval(uneval(this));
}
@DmitrySoshnikov
DmitrySoshnikov / expr.erl
Created May 19, 2014 07:33
Simple Math-Expressions parser in Erlang
-module(expr).
-export([
parse/1,
test/0
]).
% ------------------------------------------------------
%
% Simple math-expressions parser
/**
* Sum of all elements including nested arrays.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*/
[1, 2, [3, 4], 5, [6, [7, 8]]].reduce(function collect(previous, current) {
return (
(Array.isArray(previous) ? previous.reduce(collect) : previous) +
/**
* "One second".
*
* Since 1967, the sample value of One Second is:
*
* "The duration of 9,192,631,770 periods of the radiation corresponding
* to the transition between the two hyperfine levels of the ground state
* of the caesium-133 atom."
*
* by Dmitry Soshnikov <[email protected]>