Skip to content

Instantly share code, notes, and snippets.

View roninbar's full-sized avatar

Ron Inbar roninbar

View GitHub Profile
graph BT;
Object.prototype["<b>Object.prototype</b>"]
Function.prototype("<b>Function.prototype</b><br>call: <i>f</i><br>apply: <i>f</i><br>bind: <i>f</i>") --"<i>[[Prototype]]</i>"--> Object.prototype
foo(["foo: <i>f</i>"]) --"<i>[[Prototype]]</i>"--> Function.prototype
%% MyDate --"<i>[[Prototype]]</i>"--> Function.prototype
%% MyDate(["MyDate: <i>f</i>"]) -.-> |".prototype"| MyDate.prototype["<b>MyDate.prototype</b><br>getISOString: <i>f</i>"]
%% MyDate.prototype -.-> |".constructor"| MyDate
%% mydate["mydate: MyDate"] --"<i>[[Prototype]]</i>"--> MyDate.prototype --"<i>[[Prototype]]</i>"--> Object.prototype
%% anotherdate["anotherDate: MyDate"] --"<i>[[Prototype]]</i>"--> MyDate.prototype
@roninbar
roninbar / haskell.md
Last active July 27, 2022 18:09
Haskell Concepts
classDiagram

%% Values and Types
Type *-- "1..*" Value

%% Expressions
Type --o Expression : type inference
Value --o Expression : evaluation
@roninbar
roninbar / remote-port-forwarding.md
Last active March 4, 2022 09:54
SSH Port Forwarding (Remote)
sequenceDiagram
participant mysqld as MySQL Server (3306)
participant ssh as ssh
participant sshd as sshd (22) <br> on <br> example.com
participant webd as Web Server
participant browser as Browser
ssh-->>+sshd: connect(example.com:22)
ssh->>sshd: TUNNEL(localhost:3306)
sshd--&gt;&gt;sshd: listen(localhost:3306)
@roninbar
roninbar / local-port-forwarding.md
Last active March 4, 2022 09:50
SSH Port Forwarding (Local)
sequenceDiagram
participant mysql as MySQL Workbench
participant ssh as ssh
participant sshd as sshd (22) <br> on <br> example.com
participant mysqld as MySQL Server (3306) <br> on <br> example.com
ssh-->>ssh: listen(localhost:3306)
ssh-->>+sshd: connect(example.com:22)
rect rgb(240, 240, 240)
mysql--&gt;&gt;+ssh: connect(localhost:3306)
@roninbar
roninbar / perm.py
Created December 9, 2021 17:58
Find all the permutations of a string.
def perm(s: str):
return [s] if len(s) < 2 else [c + p for c in set(s) for p in perm(s.replace(c, '', 1))]
print(perm('abc'))
print(perm('fphp'))
@roninbar
roninbar / main.py
Last active December 8, 2021 19:23
Determine the mean number of die rolls needed to see all sides of the die.
import operator
from functools import partial
from itertools import accumulate, count, takewhile
from random import randrange
D = 6 # Number of die sides
N = 1_000_000 # Number of die rolls
zeros = partial(takewhile, operator.not_)
@roninbar
roninbar / proxy.ts
Last active June 27, 2021 20:57
A proxy that logs all method calls on an object.
const gl = new Proxy(rawGL, {
get(obj, key, ...rest) {
const value = Reflect.get(obj, key, ...rest);
return typeof value === 'function'
? new Proxy(value, {
apply(method, _this, args) {
console.log(`gl.${method.name}(${args.join(', ')})`);
return Reflect.apply(method, obj, args);
},
})
@roninbar
roninbar / await.js
Last active December 4, 2020 11:46
Converting async functions to ordinary functions and vice versa.
function sleep(ms) {
return new Promise(function (resolve) {
console.debug(`Sleeping for ${ms} ms...`);
return setTimeout(resolve, ms);
});
}
// "then" chain example:
function thenChainPlusLooseStatements() {
@roninbar
roninbar / actions.js
Created October 1, 2020 13:29
Async Redux Actions
export function logInAsync(username, password) {
return async function (dispatch) {
dispatch(loginPending());
const body = new URLSearchParams();
body.set('username', username);
body.set('password', password);
const response = await fetch('/user/login', {
method: 'POST',
body,
});
@roninbar
roninbar / getSqlConnection.js
Last active January 22, 2021 19:03
Lazy Initialization
/* eslint-disable dot-notation */
const mysql = require('mysql2/promise');
const debug = require('debug');
const log = debug('server:mysql');
const host = process.env['DBHOST'];
const port = process.env['DBPORT'];
const user = process.env['DBUSER'];
const password = process.env['DBPASS'];