Skip to content

Instantly share code, notes, and snippets.

@noahlt
noahlt / go_multiple_return.go
Created August 26, 2016 21:29
Passing through multiple-value-return doesn't work well in Go
package main
import "fmt"
var m = map[string]int{"a": 1, "b": 2, "c": 3}
func lookup(k string) (int, bool) {
// Can't do this :(
//
//return m[k]
@noahlt
noahlt / os_renaissance.json
Created March 29, 2017 00:32
Tracery source for OS renaissance name generator
{
"origin": ["#color# #animal# #os#"],
"color": ["Blue", "Green", "Red", "Yellow", "Orange", "Maroon", "Pink", "Black", "White"],
"animal": ["Lion", "Bear", "Seal", "Shark", "Dolphin", "Eagle", "Falcon", "Rhino", "Wolf", "Elk", "Ox", "Rabbit", "Trout"],
"os": ["OS/2", "DOS", "RISC OS", "AmigaOS", "NeXTSTEP", "BeOS", "Plan 9", "CTOS", "CP/M", "HP-UX", "TOPS-20", "Ultrix", "Multics", "Minix", "Xinu"]
}

Python’s lru_cache is sensitive to order of keyword arguments

In case you were wondering, Python’s built-in LRU cache function decorator doesn’t cache function calls with the same keyword arguments written in a different order. That is, if you cache a function and then call it twice, with the same keyword arguments but written in a different order, on the second call you won’t get the cached result and the function will be re-run.

proof/example

Let’s construct an example. We’re interested in keyword arguments, so we’ll make our function only take keyword arguments (1). We want some side-effect to tell us whether it was a cache hit or miss, so we’ll print each keyword when we iterate over it (2). We want to cache a result, so we’ll return the sum of the values of the keyword arguments (3). Finally, we apply the lru_cache decorator to the function with a maxsize=None to let the cache grow unbounded. Putting all that together, we get:

@noahlt
noahlt / oh_shit_that_sometimes_happens.txt
Created July 31, 2017 23:11
oh shit that sometimes happens: a play
OH SHIT THAT SOMETIMES HAPPENS
SAM: yo, can you help me out
CHUCK: hey what do you need
SAM: I don’t know if I told you but I recently became a camp counselor
@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
@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 / 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 / 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 / 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 / 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}: