Skip to content

Instantly share code, notes, and snippets.

@noel-yap
noel-yap / mock-first-with-rest.shlib
Last active November 16, 2025 18:19
Create a mock executable that returns a successive, caller‑provided behavior on each invocation
# shellcheck shell=bash
# mock_first_with_rest
# ---------------------
# Create a mock executable that returns a successive, caller‑provided behavior
# on each invocation, and persists an index so the next call advances to the
# next behavior. When all behaviors are exhausted, the mock prints an error to
# stderr and exits non‑zero. Re‑invoking this function for the same dependency
# resets the index back to 0.
#
@noel-yap
noel-yap / latency.md
Last active September 9, 2024 23:32
latency.md

Latency Comparison Numbers (~2020 from colin-scott.github.io/personal_website/research/interactive_latency.html)

L1 cache reference                           1 ns
Branch mispredict                            3 ns
L2 cache reference                           4 ns
Mutex lock/unlock                           17 ns
Main memory reference                      100 ns
Read 1 MB sequentially from memory       3,000 ns        3 µs
Compress 1K bytes with Zippy 2,000 ns 2 µs
#!/bin/bash
set -e
set -o pipefail
set -u
aoeu=a/o/e/u
echo "aoeu=${aoeu}" # aoeu=a/o/e/u
echo "aoeu%/*=${aoeu%/*}" # aoeu%/*=a/o/e
echo "aoeu%%/*=${aoeu%%/*}" # aoeu%%/*=a
setup() {
. "${BATS_TEST_DIRNAME}/bash-inject.shlib"
}
@test "should default to defining function" {
assert_equal "$(declare -F fn)" ''
@inject "${BATS_TEST_DIRNAME}/fn.sh"
run echo "$(declare -f fn)"
#!/usr/bin/python3.8
import argparse
import math
def memoize(f):
memo = {}
#!/usr/bin/python3
import argparse
def sum_of_sequence(n):
return n * (n + 1) / 2
def sum_of_squares_of_sequence(n):
#!/usr/bin/python3
from typing import TextIO
try:
triangle: TextIO = open('triangle.txt')
tree = [[int(s) for s in line.split()]
for line in [line.rstrip() for line in triangle.readlines()]]
#!/usr/bin/python3
from collections import Counter
from enum import Enum
from functools import total_ordering
from typing import List, TextIO, Dict
@total_ordering
class Card:
class Triple {
public final int a;
public final int b;
public final int c;
private Triple(final int a, final int b, final int c) {
this.a = a;
this.b = b;
this.c = c;
}
enum Coin {
ONE_P(1),
TWO_P(2),
FIVE_P(5),
TEN_P(10),
TWENTY_P(20),
FIFTY_P(50),
ONE_L(100),
TWO_L(200);