Skip to content

Instantly share code, notes, and snippets.

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

@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 / 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 / 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 / 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 / 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 / es6-delegating-generator.js
Last active April 7, 2018 12:31
ES6 delegating generator
/**
* ES6 delegating generators.
*
* ES6 dramatically simplified calling of nested generators
* using "yield *" construct, which is called delegating
* generator.
*
* From history:
* Previously (in the original SpiderMonkey/Python) implementation
* we had to maintain a manual stack for it. The technique was
@DmitrySoshnikov
DmitrySoshnikov / stack-vm.js
Last active April 29, 2024 01:56
Educational Stack-based Virtual Machine
/**
* Educational Stack-based VM.
*
* See also:
* - More complex example with recursion: https://gist.github.com/DmitrySoshnikov/afda459222e96e6002ac
* - Register-based VM example: https://gist.github.com/DmitrySoshnikov/6407781
*
* by Dmitry Soshnikov <[email protected]>
* http://dmitrysoshnikov.com
* MIT Stye License (C) 2015
@DmitrySoshnikov
DmitrySoshnikov / stack-vm-recursion.js
Last active March 23, 2022 07:09
Educational Stack-based VM with recursion example
/**
* Educational Stack-based VM.
*
* See also:
* - A simplified example without recursion: https://gist.github.com/DmitrySoshnikov/76e1cfb930df8d36145c
* - Register-based VM example: https://gist.github.com/DmitrySoshnikov/6407781
*
* by Dmitry Soshnikov <[email protected]>
* http://dmitrysoshnikov.com
* MIT Stye License (C) 2015
@DmitrySoshnikov
DmitrySoshnikov / shadow-hoisting-tdz.md
Last active August 29, 2015 14:15
Shadow, hoisting, TDZ

1. A system without hoisting

An example of Scheme program without hoisting:

(define x 10)     ; global `x`
(print x)         ; 10

((lambda ()
    (print x)     ; still 10, not shadowed yet