Skip to content

Instantly share code, notes, and snippets.

@bsidhom
bsidhom / integer-partitions-internal.js
Created December 6, 2025 07:45
Integer partitions in JavaScript with internal iteration
// Turns out JS can be fast. Generators are just slow. :(
// A fairer comparison with
// https://gist.github.com/bsidhom/32162a78e602d2f620dc8ab07c71ef97
const main = () => {
let i = 0;
partitionsInternal(100, (_p) => {
i++;
if (i % 1000000 == 0) {
console.log(i);
}
@bsidhom
bsidhom / integer-partitions.rs
Created December 6, 2025 07:36
Integer partitions with internal iteration
// Compare to https://gist.github.com/bsidhom/224b1c343bc18e4c351f3ba6a3655dba, which uses external
// iterators. This is _substantially_ faster than the JavaScript variant (unsurprisingly):
// https://gist.github.com/bsidhom/53b13d54f0059086d9b6c4e20fd8d42a
fn main() {
let mut n = 0;
partitions(100, |_p| {
n += 1;
if n % 1000000 == 0 {
println!("{n}");
}
// Translation of https://gist.github.com/bsidhom/c136b6bd26dcff64d9ca443a5bd022a3 into JS for use in the browser.
const main = () => {
const n = 10;
for (const partition of genPartitions(n)) {
console.log(partition);
}
};
const genPartitions = function* (n) {
const prefix = [];
// This naively computes partitions of a _multiset_ by first creating a unique
// set of indices (into that multiset) and generating all partitions of those
// (necessarily unique) indices. Finally, multi-partitions are canonicalized,
// deduplicated, and then printed. This only works for very small multisets
// since the set of all partitions must fit into memory. It is essentially only
// useful for verifying that the final algorithm does what it's supposed to do.
//
// My tentative strategy for the memory-friendly version is to build all
// partitions in _lexicographic_ order, recursively (top-down). The general idea
// is to specify the initial set in lexicographical order. Starting at the end,
@bsidhom
bsidhom / set-partitions.js
Last active November 14, 2025 04:35
Generate all partitions of a set
// NOTE: Building this up en route to a hopefully-clean _multiset_ partition generator.
const main = () => {
const set = [1, 2, 3, 4];
for (const partition of genPartitions(set)) {
console.log(partition);
}
};
// Generate all partitions of the given set (given as an array of unique items).
// If the items are given in _ascending_ order, then the generator will yield
@bsidhom
bsidhom / ocrmypdf-podman.sh
Created October 26, 2025 21:06
Run ocrmypdf via podman with working directory auto-mounted.
# On macOS, certain directories such as /tmp are specially linked and must be referenced by canonical paths under volume mounts.
podman run --rm -i --user "$(id -u):$(id -g)" --userns keep-id --workdir /data -v "$(readlink -f $(pwd)):/data" jbarlow83/ocrmypdf-alpine <args>
#!/usr/bin/env python3
import decimal
import math
from decimal import Decimal
from fractions import Fraction
# In response to https://www.reddit.com/r/mildlyinteresting/comments/1hcb9ce/not_a_single_person_at_my_2000_student_high/
# and, in particular, some sloppy math in https://www.reddit.com/r/mildlyinteresting/comments/1hcb9ce/comment/m1mzfgh/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
@bsidhom
bsidhom / powerset.hs
Created November 5, 2024 05:49
Powerset construction for comparing runtime and memory performance characteristics between Rust, Haskell, and Python
module Main where
main :: IO ()
main = do
mapM_ print xs where
xs = powerset [(1 :: Integer)..24]
powerset :: [a] -> [[a]]
powerset = pset [] where
pset :: [a] -> [a] -> [[a]]
@bsidhom
bsidhom / fixfit.py
Last active October 10, 2024 03:48
Inspect and strip fields from FIT activity files
#!/usr/bin/env python3
# This tool operates on CSV files as created (and read) by the FitCSVTool.jar
# tool included in the FIT SDK. Input is always read from stdin, and output is
# written to stdout.
#
# Example usage:
# java -jar FitCSVTool.jar -b original_activity.fit original_activity.csv
# ./fixfit.py detect <original_activity.csv
# ./fixfit.py strip --field=accumulated_power --field=avg_power --field=functional_threshold_power --field=max_power --field=normalized_power --field=power --field=power_zone_high_boundary --field=threshold_power --field=time_in_power_zone <original_activity.csv >fixed_activity.csv
@bsidhom
bsidhom / update-beryl-tailscale.sh
Created September 27, 2024 04:56
Update GL.iNet Beryl AX (GL-MT3000) tailscale version
#!/usr/bin/env bash
set -euo pipefail
function main() {
if [[ $# -ne 1 ]] ; then
echo "usage: $0 <router ip>" >&2
exit 2
fi
local router_ip="$1"