Skip to content

Instantly share code, notes, and snippets.

View wolever's full-sized avatar

David Wolever wolever

View GitHub Profile
@wolever
wolever / type-safe-object-functions.ts
Created December 2, 2024 00:08
Type-safe definitions of `Object.keys`, `Object.entries`, and `Object.values`: `objectKeys`, `objectEntries`, and `objectValues`
/**
* ObjectType matches any "regular" object.
*
* For example:
* > {} satisfies ObjectType
* > { a: 1, b: 2 } satisfies ObjectType
* > [] satisfies ObjectType
* > null satisfies ObjectType
* TypeError: 'null' does not satisfy 'ObjectType'.
* > undefined satisfies ObjectType
@wolever
wolever / fractional-indexing-with-floats.rst
Last active October 21, 2023 17:53
Analysis of floating point fractional indexing

Fractional indexing is a technique for assigning a rank to ordered items, where an item's position can be changed - or a new item introduced - with only one rank value update.

For example, given the items:

{ name: "a", rank: 1 }
{ name: "c", rank: 2 }

With a naive rank ordering scheme, introducing "b" in between "a" and "c" would

@wolever
wolever / change_logs.sql
Created July 26, 2022 18:10
`change_logs.sql` - automatically track changes to Postgres tables.
/*
change_logs: use triggers to automatically track changes to certain columns in
database tables.
To enable change tracking for a table:
select change_logs_track_table(
-- table name
'table_to_track',
-- primary key column
@wolever
wolever / battleship.html
Created September 21, 2021 05:56
Implementation of probabilistic Battleship playing algorithm roughly as described in https://www.datagenetics.com/blog/december32011/
<html>
<head>
<script src="./battleship.js"></script>
<style>
.board,
.heatmap {
border: 1px solid black;
margin: 5px;
display: inline-block;
}
@wolever
wolever / machine.js
Last active April 23, 2020 03:07
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'KNIT',
initial: 'idle-no-flow',
states: {
'idle-no-flow': {
on: {
FLOW_ASSIGN: 'idle-with-flow'
}
},
/**
* Catches any exception which might be raised by a promise and returns a
* tuple of [result, error], where either the result or the error will be
* undefined:
*
* let [res, error] = await maybe(someApi.get(...))
* if (err) {
* return `Oh no there was an error: ${err}`
* }
* console.log('The result:', res)
@wolever
wolever / fix-mouse.lua
Created November 7, 2019 18:17
Hammerspoon config to use mouse button 3 for scrolling, remap button 4 to middle button
-- HANDLE SCROLLING WITH MOUSE BUTTON PRESSED
local scrollMouseButton = 3
local deferred = false
overrideOtherMouseDown = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDown }, function(e)
-- print(hs.eventtap.event.properties['mouseEventButtonNumber'])
local mouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber'])
if mouseButton == scrollMouseButton then
deferred = true
return true
CREATE OR REPLACE VIEW accounts.address_token_balances AS (
SELECT
address_hash,
token_contract_address_hash,
block_number,
LEAD(block_number, 1, 2147483647) OVER (
PARTITION BY address_hash, token_contract_address_hash
ORDER BY block_number ASC
) as next_block_number,
SUM(amount) OVER (
#!/usr/bin/env node
// My modifications to this code are public domain
const { writeSync, readFile } = require("fs")
const async_hooks = require("async_hooks")
function showStack() {
let e = new Error()
writeSync(1, e.stack.split('\n').slice(2).join('\n') + '\n')
@wolever
wolever / watchdir
Created October 24, 2018 18:50
Watch a directory for changes and run a command when it does
#!/bin/bash
# STOP! Before going any further, think: are you going to regret the decision
# to write this script?
# Deciding to write this in bash was not one of my better decisions.
# -- https://twitter.com/alex_gaynor/status/369892494114164736
IFS="`printf "\n\t"`"
set -u
if [[ "$#" -lt 2 ]]; then