Skip to content

Instantly share code, notes, and snippets.

View peter-leonov's full-sized avatar

Peter Leonov peter-leonov

View GitHub Profile
@peter-leonov
peter-leonov / lambda-queue.js
Last active October 18, 2017 23:07
Serial queue solution using lambda calculus.
const empty =
run =>
run()
const enqueue =
(q, fn) =>
next =>
q(() => fn(next))
let q = empty
@peter-leonov
peter-leonov / react-redux_v5.x.x.js
Last active December 12, 2018 19:25
react-redux connect() typings based on Sam's Goldman example
// see here for details
// https://medium.com/@samgoldman/ville-saukkonen-thanks-and-thanks-for-your-thoughtful-questions-24aedcfed518
// https://github.com/facebook/flow/issues/7125
/*
S = State
A = Action
OP = OwnProps
SP = StateProps
DP = DispatchProps
@peter-leonov
peter-leonov / bomb.sh
Last active November 22, 2019 03:46
docker fork bomb
docker run ruby ruby -e 'loop { fork { fork { sleep 0.1 } } }'
@peter-leonov
peter-leonov / async.js
Created February 16, 2020 21:49
Re-implementing a simple (but with a whole for loop!) async function as a generator and as a co-routine for fun
// lib
const sleep = t => new Promise(res => setTimeout(res, t));
const getRandom = () => sleep(100).then(() => Math.random());
// native solution
async function getRandomsNative(count) {
await sleep(100);
const randoms = [];
const { openSync, readSync } = require('fs')
const DEV_RANDOM = openSync('/dev/random')
function getVeryRandomString(length) {
const buf = Buffer.alloc(length)
readSync(DEV_RANDOM, buf, 0, length)
return buf.toString()
}
@peter-leonov
peter-leonov / naive-event-loop.js
Last active April 6, 2020 18:25
Pure JS event loop implementation with only timers working
// timer.js
function main(global) {
global.mySetTimeout(function () {
console.log("500ms");
}, 500);
global.mySetTimeout(function () {
console.log("100ms");
}, 100);
@peter-leonov
peter-leonov / stream.js
Last active April 13, 2020 08:39
Array<Promise<T>> -> AsyncIterator<T>
async function main() {
async function f1() {
return new Promise((res) => {
setTimeout(() => {
res("1.1");
}, 1000);
});
}
async function f2() {
@peter-leonov
peter-leonov / closure.js
Created April 20, 2020 18:18
Primitive closure implementation in JS to illustrate that closure is "just" an object
class RootScope {
constructor() {
this.map = new Map();
}
add(name, value) {
return this.map.set(name, value);
}
set(name, value) {
@peter-leonov
peter-leonov / ast.js
Last active April 29, 2020 21:54
Naive micro JS parser (with parser <-> lexer connection)
foo / 1 + 2 * 3 + 4 ; bar = /rex1/ /* bla-bla */ / /rex2/
// gives this naive AST
({
type: 'program',
body: [
{
type: 'statement',
body: {
type: 'expression',
@peter-leonov
peter-leonov / base64.js
Created July 26, 2020 13:45
Simple not-byte-precise base64 encoder
const CODE_A = "A".charCodeAt(0);
const CODE_a = "a".charCodeAt(0);
const CODE_0 = "0".charCodeAt(0);
const CODE_p = "+".charCodeAt(0);
const CODE_s = "/".charCodeAt(0);
// Dear v8, inline this please
const toA = (n) =>
n === 63
? CODE_s