Meta

This is the first in a series of TIL posts. Each post will contain things I learned over the previous week as well as things I don’t really understand.

Closures

a closure closes over something, capturing the state at its declaration. consider a C++ lambda:

int main()
{
    int x = 42;

    auto fn = [x]() { return x; };
    x++;

    assert(43 == x);
    assert(42 == fn());
}

fn closes over x, binding its copy of x to 42. fn is not affected by the increment performed on x.

Indenting

> and < will indent and unindent text in vim(1). This is much more effective than my previous method of hitting = and hoping for the best.

Tuple unpacking

python’s tuple unpacking is really nice. For a while I was writing code like this:

def foo(x, y):
	print(f'({x},{y})')

pair = (1, 0)
foo(pair[0], pair[1])

python’s tuple unpacking simplifies this by allowing:

foo(*pair)

Writing Unix

Ken Thompson wrote Unix in 3 weeks; one week for the shell, one week for the editor, one week for the assembler[^1].

I still don’t understand

C++ Type erasure. Well I understand type erasure, at least in the context of providing a type for a lambda, but it still seems like it will always require heap allocation. Maybe I need to watch Arthur O’Dwyer’s talk again.


[^1] Brian talks about this in Chapter 2 of UNIX: A History and a Memoir