Skip to content

Instantly share code, notes, and snippets.

@jabney
jabney / arrayView.ts
Last active February 16, 2024 20:09
Implementation
const normalizeStart = (length: number, start: number | undefined): number => {
start = start ?? 0;
if (start < 0) {
start = start + length;
} else if (start < -length) {
start = 0;
} else if (start >= length) {
start = length;
}
@jabney
jabney / micro-redux.js
Last active February 15, 2024 17:03
micro-redux: a minimal implementation of Redux createStore, with thunkish behavior added in
/**
* Minimal implementation of Redux createStore, with thunkish behavior.
*/
import subject from './micro-subject' // see micro-subject.js gist
/**
* @template T
* @typedef {import('./subject').Subject<T>} Subject
*/
/**
@jabney
jabney / micro-subject.js
Last active February 15, 2024 17:03
micro-subject: provide basic subscribe/notify/unsubscribe functionality in a dozen or so lines of code
/**
* Provide basic subscribe/notify/unsubscribe functionality in a
* dozen or so lines of code.
*/
/**
* @template T
* @typedef {(value?: T) => void} ObserverFn
*/
@jabney
jabney / entropy.js
Last active January 29, 2024 23:38
Javascript implementation of a Shannon entropy calculation in bits per symbol
// entropy.js MIT License © 2014 James Abney http://github.com/jabney
/***************************************
* ES2015
***************************************/
// Shannon entropy in bits per symbol.
function entropy(str) {
const len = str.length
@jabney
jabney / setOps.js
Last active February 1, 2023 10:55
Fast JavaScript set operations: union, intersection, difference, complement, and equals. Includes support for objects.
// setOps.js MIT License © 2014 James Abney http://github.com/jabney
// Set operations union, intersection, symmetric difference,
// relative complement, equals. Set operations are fast.
(function(so) {
'use strict';
var uidList = [], uid;
// Create and push the uid identity method.
@jabney
jabney / random-seed.js
Last active May 9, 2022 15:10
Seedable random numbers using a hash
"use strict";
const crypto = require("crypto");
/**
* @param {string|number} [seed]
* @param {number} [offset]
*/
const createRng = (seed = 0, offset = 0) => {
const numBytes = 6; // 48 bits
const maxInt = 2 ** 48;
@jabney
jabney / timer-cache.ts
Created May 9, 2022 14:39
An example of class composition with a timed value cache
interface IValueCache<T> {
get: () => T;
invalidate: () => void;
}
/**
* A simple cache that invalidates based on a timer. Every access of
* the value will refresh the timer.
*
* Even this simple class can be broken up into useful pieces and then
@jabney
jabney / array-view.ts
Last active May 9, 2022 14:36
An experimental view that can iterate over a subset of an array
/**
* An experimental view that can iterate over a subset of an array.
* - Not unit tested, there are undoubtedly bugs.
*/
"use strict";
const clamp = (min: number, max: number, value: number) => {
return Math.min(max, Math.max(min, value));
};
@jabney
jabney / multithreaded-pi.cpp
Last active January 22, 2021 23:36
Calculate PI using Leibniz algorithm spread across multiple threads
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
using namespace std;
@jabney
jabney / micro-react-redux.js
Last active January 22, 2020 20:27
micro-react-redux: a minimal implementation of react-redux style provider and connect function
/**
* Minimal implementation of react-redux style provider and connect function.
*/
import React, { useState, useEffect, useContext } from 'react'
const StoreContext = React.createContext(null)
/**
* Wraps root component for providing store context, e.g.,
*