Skip to content

Instantly share code, notes, and snippets.

@jabney
jabney / arg-parser-stateless.ts
Last active November 26, 2024 03:07
arg-parser: A stateless, minimally opinionated argument parser
/**
* A minimally opinionated argument parser with a stateless implementation.
*
* Author: James Abney
* License: MIT
*/
import { Tokenizer, TokenSpec } from './tokenizer'
export interface IArg {
type: 'arg'
@jabney
jabney / arg-parser.ts
Last active November 23, 2024 16:25
A minimally opinionated argument parser
/**
* A minimally opinionated argument parser.
*
* Author: James Abney
* License: MIT
*/
import { Tokenizer, TokenSpec } from './tokenizer'
const tokens: TokenSpec<TokenType>[] = [
[null, /^\s+/], // whitespace
@jabney
jabney / tokenizer.ts
Last active November 26, 2024 03:08
tokenizer: a single token lookahead tokenizer
/**
* A single token lookahead tokenizer.
*
* Author: James Abney
* License: MIT
*/
export type Token<T extends string = string> = { readonly type: T; readonly value: string }
export type TokenSpec<T extends string = string> = readonly [name: T | null, match: RegExp] // empty name tokens are thrown away.
export class Tokenizer<T extends string = string> {
@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 / 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 / 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 / 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-reselect.js
Last active January 22, 2020 20:25
micro-reselect: memoize a function based on the state passed to it (mimics reselect npm module)
/**
* Implement a simplified createSelector function mimicking the reselect module.
*
* Memoizes the result of a function called with a state object as its argument.
* The given function is only called when state values change.
*/
/**
* A resolver returns a value from a state object (key/value pairs).
*
@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.,
*