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 / code.sh
Created October 23, 2011 07:19
How do I kill whatever process is listening to a particular port?
function killport {
if [ $1 == '-h' ] || [ -z $1 ]; then
echo '`killport <PORT>` finds the process listening to the specified port and kills it.'
else
process_line=`sudo lsof -i :$1 | tail -1`
if [ "$process_line" == "" ]; then
echo "no processes listening on $1"
else
process_name=`echo "$process_line" | awk '{print $1}'`
echo "killing $process_name"
@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 / remote_env_vars.sh
Created May 17, 2016 23:34
Using environment variables from remote servers
#!/usr/bin/env bash
# A couple of handy bash functions that I have in my .bashrc:
# get_env uses ssh to copy an environment variable from a remote
# server to your current machine. Example usage:
#
# get_env SOME_VAR_NAME example.com
get_env () {
VARNAME=$1