Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
justanotherdot / clone-closure.rs
Created August 4, 2020 23:15
FnOnce (auto inferred closure?) cloneable.
fn main() {
let xs = vec![1];
let ys = xs.clone();
let clos = move || xs;
assert_eq!(&(clos.clone())(), &ys);
assert_eq!(&clos(), &ys);
}
@justanotherdot
justanotherdot / cartesian.rs
Last active August 11, 2020 23:02
Simple 2-dimensional cartesian product using std::iter::from_fn.
fn main() {
let xs = vec![1, 2, 3];
let ys = vec![6, 5, 4];
let mut i = 0;
let mut j = 0;
let iter = std::iter::from_fn(|| {
if i >= xs.len() {
return None;
}
let rv = Some((xs[i], ys[j]));
#!/usr/bin/awk -f
# This program is a copy of guff, a plot device. https://github.com/silentbicycle/guff
# My copy here is written in awk instead of C, has no compelling benefit.
# Public domain. @thingskatedid
# Run as awk -v x=xyz ... or env variables for stuff?
# Assumptions: the data is evenly spaced along the x-axis
# TODO: moving average
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@justanotherdot
justanotherdot / posix-readlink
Created July 7, 2020 01:25
Show an absolute path given a relative path in a POSIX compliant way.
@justanotherdot
justanotherdot / bisect
Last active June 24, 2020 23:06
Only run bisect tests on GitHub flavored merge quests.
#!/bin/sh -eu
FROM_COMMIT="$1"
TO_COMMIT="$2"
git bisect start "$FROM_COMMIT" "$TO_COMMIT"
git bisect run ./bisect-test && git bisect reset
@justanotherdot
justanotherdot / git-init-to-main
Created June 16, 2020 05:31
Change `git init`s default behavior to use `main` branch.
#!/bin/sh -eu
echo '=== Changing `git init` to default to "main" branch.'
echo "+ Updating ~/.gitconfig"
cat <<'EOF' >> ~/.gitconfig
[init]
templateDir = ~/.config/git/template/
EOF
ROOT_TEMPLATE_DIR=""
@justanotherdot
justanotherdot / rename-default-main
Last active June 16, 2020 22:13
Rename local and remote to
#!/bin/sh -eux
# usage:
# cd into where you keep your git repositories then run this script.
# if you want to only update a single repository, comment out the loop
# below and calling this script from within the project you want changed.
update() {
git stash
# If you use something like GitHub, you will also
#!/bin/sh -eu
announce() {
S="$1"
S_LEN="$(echo $S | wc -m)"
TERM_WIDTH="$(tput cols)"
TERM_WIDTH="$(( TERM_WIDTH - 6 ))"
printf "===%*s%*s===\n" $(( (TERM_WIDTH + S_LEN) / 2 )) "$S" $(( (TERM_WIDTH - S_LEN) / 2 )) ""
}
@justanotherdot
justanotherdot / x-compile-test
Last active March 21, 2023 16:59
Attempt to build all cross-compile targets supported by `rustc` and report the time taken.
#!/bin/sh -eu
FILTER="${FILTER:="."}"
TOOLCHAIN_BK="$(rustup toolchain list | rg default | awk '{print $1}')"
TOOLCHAIN_KIND_BK="$(echo "$TOOLCHAIN_BK" | rg -o '^(stable|nightly)')"
if [ "$TOOLCHAIN_KIND_BK" != "stable" ]; then
rustup install stable
rustup default stable
fi