Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@noahlt
noahlt / values_equal_regardless_of_type.dart
Last active November 6, 2021 22:40
Are variables of the same value but different types equal?
typedef A = String;
typedef B = String;
void main() {
A a = "xxx";
B b = "xxx";
if (a == b) {
print("bad: variables with the same value are equal");
} else {
print("excellent! variables with different types are not equal");
@noahlt
noahlt / useThrottledState.js
Created January 4, 2021 22:40
React Hook for throttled state value
// Usage:
const [value, setValue] = useThrottledState(0, 1000, true);
// As long as `live` is true, every `updateInterval` milliseconds, `value` will
// get updated (causing the calling component to re-render). Meanwhile, you can
// call `setValue` as frequently as you want without causing a re-render each time.
function useThrottledState(initialValue, updateInterval, live) {
const realValueRef = useRef(initialValue);
const setRealValue = (xf) => {
realValueRef.current = isFunction(xf) ? xf(realValueRef.current) : xf;
};
@noahlt
noahlt / home_wifi_multiple_routers.md
Last active September 27, 2020 18:01
Setting up Home Wifi with Multiple Routers

Setting up home wifi with multiple routers

The setup: one gateway ethernet router with two wifi routers attached, to provide wifi coverage throughout the house. Is this better than buying a brand-new 3-pack of eero routers? Probably not, but because I already had two good wifi routers on hand this was a lot cheaper.

Disclaimer: I am not a networking expert, but this is what worked for me.

  • seems okay to have multiple routers broadcasting the same SSID
  • consumer routers try to be too clever. Using their “bridge mode” never worked.
  • the important end-state configuration on each wifi router:
  • plug the upstream connection into a normal ethernet port, not the WAN port.
curl -s https://www.purpleair.com/json?show=$SENSOR_ID | jq '.results[] | .PM2_5Value' | sed -e 's/^"//' -e 's/"$//' | awk '{ total += $1; count++ } END { print total/count }'
@noahlt
noahlt / setter_component_hook.md
Created August 24, 2020 15:44
Setter Component Hook

Playing with React over the weekend, I made a hook that’s like useState except instead of returning a setter function it returns a component you can render:

function usePercentageSlider(label, initialValue) {
  const [value, setValue] = useState(initialValue);
  return [
    value,
    <div>
      <div>
        {label} {value}:
@noahlt
noahlt / map_maps.js
Created July 23, 2019 23:50
Make a method to map a Map to an Object
Object.fromMap = function(m, f) {
var r = {};
f = f || ((x, y) => [x, y]);
for (let [k, v] of m) {
[k, v] = f(k, v);
r[k] = v;
}
return r;
}
@noahlt
noahlt / knuth_success.md
Created April 10, 2019 16:43
Don Knuth on purpose and success

As I was designing a chip for a very simple RISC computer, I was surprised to find that the easiest and somehow the best way to design this chip was to have it doing all kinds of things that would never be needed afterwards. I mean, two binary numbers were input to the chip at each clock cycle, and the adder would add them and the subtracter would simultaneously subtract them, and the multiplier would multiply them. These things were all going on at once inside the chip, but only one of those results would survive and actually be used in the computation in the next step. In this way the chips were operating quite differently from the computer programs I had been writing before.

The alternative would have been to design the chip so that every circuit inside the multiplier had extra inhibitors on it saying, “Don’t multiply unless I tell you to.” That would add an awful lot to the hardware.

I started thinking about this as an interesting metaphor for society and the world in general. It might help us to r

@noahlt
noahlt / iteration.md
Created April 1, 2019 21:20
Iteration

Iteration with break:

var attempt = 1;
while (true) {
    var [succeeded, error] = await f()
        .then(() => [true])
        .catch(error => [false, error]);

    attempt++;

if (succeeded || attempt > maxAttempts)

@noahlt
noahlt / print_kb_layout.sh
Created September 27, 2017 23:00
Print keyboard layout in X11
setxkbmap -query | grep layout | sed 's/layout: *\(.*\)/\1/'
@noahlt
noahlt / zsh_chinese_yn_prompt.md
Last active September 10, 2017 18:34
zsh Chinese yn prompt

The other day I saw this on Twitter:

zsh: 没想到吧,我还学了中文。

— Makito@nil (@sumimakito) September 8, 2017

Makito's Tweet says "This hadn't occurred to me, and I even know Chinese." In the screenshot, [Oh My Zsh][0] has given him a Y/n prompt asking whether he wants to enable automatic updates. He has responded by typing "成", which (in this context) means "OK!". Normally I would expect this sort of command-line prompt to assume anything that's not Y, y, YES, or yes to be a negative answer, but, lo and behold, in his screenshot Oh My Zsh understands his Chinese response and begins an update! Here's a plain text transcript:

[Oh My Zsh] Would you like to check for updates? [Y/n]: 成
Updating Oh My Zsh