Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
DmitrySoshnikov / reference-counting-gc.js
Last active September 30, 2022 00:19
Reference counting GC
/**
* Automatic Reference Counting (ARC) Garbage Collection technique.
*
* This diff describes Reference counting GC technique.
* See also previous lesson on Mark and Sweep GC:
* https://gist.github.com/4391763
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*/
@DmitrySoshnikov
DmitrySoshnikov / stop-copy-gc.js
Last active February 13, 2018 19:23
Stop and Copy GC technique.
/**
* Stop and Copy Garbage Collection technique.
*
* See also previous lessons on
*
* - Mark and Sweep GC: https://gist.github.com/DmitrySoshnikov/4391763
* - Reference Counting (ARC) https://gist.github.com/DmitrySoshnikov/4646658
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License

List comprehensions

Basic idea in theoretical programming: a notation for Set builders from math.

Basic idea in practical programming: syntactic sugar for "map + filter" functional methods:

Example of map and filter, ES6

let x = N.filter(x => x > 10).map(x => x * x);
"use strict";
const slice = [].slice;
/**
* A user callback function that is raised when the result of an async function
* is available.
*
* @param Any error
* If truthy, indicates an error result. Will be thrown from the
@DmitrySoshnikov
DmitrySoshnikov / reg-vm.js
Last active June 15, 2018 20:38
A simple educational Register-based VM, Assembler, and Disassembler.
// "Fetch, decode, eval!"
// A simple Register-based VM, Assembler, and Disassembler.
// by Dmitry Soshnikov <[email protected]>
// This virtual machine (VM) consists of registers (data storage),
// and operations (instructions) which operate on the registers.
// --------------------------------------------------------------
// Registers.
// --------------------------------------------------------------
/**
* A simple Time travel machine to the future.
*
* A theoretical time-traveling to the future
* (a very-very-very simplified explanation/application
* of the General relativity).
*
* .allgemeine Relativitätstheorie
*
* by Dmitry Soshnikov <[email protected]>
/**
* "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]>
/**
* 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) +
@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
/**
* 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));
}