Skip to content

Instantly share code, notes, and snippets.

@bshlgrs
bshlgrs / threadlocals_are_a_lie.c
Last active October 13, 2016 18:18
This is a demonstration that threadlocal storage can be written to by other threads.
#include <pthread.h>
#include <stdio.h>
__thread int very_local = 5;
/* this function is run by the second thread */
void *do_other_thing(void *very_local_pointer)
{
printf("in thread 2, very_local is %d\n", very_local);
*(int*) very_local_pointer = 10;

Summary: I investigated a variety of possible pro-environment actions, and concluded that none of them were worth my time. I don’t think that any pro-environmental actions are as time/money efficient as giving to the best climate change charities, and I don’t think that those climate change charities are anywhere near the best human-oriented charities we currently have available as donation targets. I briefly consider non-direct effects of pro-environmental actions.

People often tell me I should care about the environment, and take steps to protect it. I’m on board with that in principle. However, before I spent a few hours researching this, I had no idea of how to judge how much environmental damage is caused by different things I’m doing, and which supposedly environmentally friendly measures I should bother taking.

For example, I’m aware that

list of programming project ideas

Board games

Any two player board game is a good exercise for basic programming logic, and you can make it into a console application. Checkers, Connect-4, Othello, Poker, Go, whatever. Chess is somewhat harder than those other games.

Here are some ways to extend these projects:

  • Add the ability to save and load games
  • Add an AI
# Returns the number of items in arr smaller than x.
def rank(arr, x, start_idx = 0, end_idx = nil)
return 0 if arr.empty?
end_idx = arr.length if end_idx.nil?
# this makes sense I promise, I'll explain later
return arr.length if start_idx == arr.length
return 0 if end_idx == 0

fun data structure projects

  • Binary search trees! There are lots of little-known self-balancing BST implementations. Basically no-one has heard of any except AVL trees, red-black trees, and BTrees.
    • Implement a lesser-known BST in a language of your choice; figure out how fast it is; try to make it as fast as a reference implementation. Try to find a semi-realistic application where your implementation is faster than a competitor. (For example, splay trees probably outperform red-black trees in cases with serious temporal locality.) This project is definitely possible.
  • Scapegoat tree
layout title date
post
Basic scaling advice
2016-03-11

So you’ve made a pretty standard web application which currently has two components: a database and an application server. Your website suddenly gets super popular. How do you scale it? Here’s my opinionated and incomplete answer.

The first step in scaling a system is to make sure you have thorough monitoring and logging. This way, when something breaks, you’ll know what it is. Common tools for this include NewRelic, Nagios, and platform specific tools on AWS

how to play songs on piano

A friend asked me how I go about playing songs on piano just from their chords. Here are the preliminary notes on the subject which I sent her:

When I'm playing a piano, I switch between several different behaviors:

  • playing a bassline with my left hand and chords with my right. This is my default behavior.
  • Playing riffs (eg if I were playing Gronlandic Edit, most of my time would be spent playing the bassline).
  • playing parts which are copied note-for-note from the original piece, because they’re intricate or important enough that I can’t replace them. Eg playing the intro for Sirens Of Your Toxic Spirit.
data CombinatorialProblem = ChooseOneOfNOptions Int
| OrderNThings Int
| And CombinatorialProblem CombinatorialProblem
| Or CombinatorialProblem CombinatorialProblem
| RepeatedChoice CombinatorialProblem Int
-- how many ways can eight people sit next to each other?
eightPeopleSitting = OrderNThings 8
import scala.collection.mutable
class MinStack {
val myStack = mutable.Stack[Int]
def push(item: Int) = myStack.push(item)
def pop() = myStack.pop()
// BAD! Linear time
case class Loanable[A](loan: A, close: () => Unit) {
def foreach[B](f: A => B): Unit = {
f(loan)
close()
}
def map[B](f: A => B): Loanable[B] = {
new Loanable(f(loan), close)
}