Skip to content

Instantly share code, notes, and snippets.

@akojo
akojo / main.go
Created January 21, 2022 16:26
How to write a webserver in Go
package main
import (
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {

Keybase proof

I hereby claim:

  • I am akojo on github.
  • I am akojo (https://keybase.io/akojo) on keybase.
  • I have a public key ASC1-w8C4fZJzpU7NIECYLAwc-qvHPf1zwe_fP_62RuN9Ao

To claim this, I am signing this object:

@akojo
akojo / Makefile
Last active February 10, 2020 14:48
Get your C/C++ project dependencies correct right from the start
quiet := $(if $V,,@)
define to-deps
$(join $(dir $1),$(addprefix .,$(addsuffix .d,$(notdir $1))))
endef
define make-compile-command
%.o: %.$1
@echo COMPILE.$1 $$<
$(quiet)$$(COMPILE.$1) -MM -MF $$(call to-deps,$$<) -MP -MT $$@ $$<
@akojo
akojo / juicy.cpp
Created July 27, 2015 13:22
Coding puzzle: variations on a theme
#include <algorithm>
#include <codecvt>
#include <cwctype>
#include <fstream>
#include <iostream>
#include <iterator>
#include <locale>
#include <regex>
#include <set>
#include <sstream>
@akojo
akojo / search-test.c
Last active August 29, 2015 14:20
Rundown of some basic search algorithms using 32-bit values as keys
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define TEST_NROUNDS 2e6
#define TEST_NITEMS 10000
@akojo
akojo / bin.c
Created January 28, 2015 07:11
Binary pack/unpack in C
#include <arpa/inet.h>
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
uint8_t *bin_pack(uint8_t *buf, size_t size, const char *fmt, ...)
{
uint8_t *end = buf + size;
const char *fp = fmt;
int be = 0;
@akojo
akojo / buildloc
Last active August 29, 2015 14:07
Project-specific location databases
#!/bin/sh
db=.location.db
dblist=~/${db}.list
updatedb -l 0 -U . -o $db
if [ -w $dblist ]; then
tmplist=$(mktemp)
echo $(pwd)/$db | cat $dblist - | sort -u > $tmplist
@akojo
akojo / mad_max.cpp
Last active August 29, 2015 14:05
std::max not behaving quite like it should
#include <cassert>
#include <iostream>
struct Message {
int priority;
std::string payload;
friend bool operator<(const Message& a, const Message& b)
{
return a.priority < b.priority;
@akojo
akojo / dynarray.c
Created June 23, 2014 08:43
Branch-free dynamic arrays
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
struct array;
struct array *array_create(int initsz);
void array_push(struct array *array, int elem);
static void push_unsafe(struct array *array, int elem);
static void push_expand(struct array *array, int elem);
@akojo
akojo / hello.cpp
Created December 4, 2013 12:42
A somewhat contrived hello world in C++
#include <iostream>
using namespace std;
string str(char c) { return string(1, c); }
template <typename C, typename... Rest>
string str(C c, Rest... Args) { return string(1, c) + str(Args...); }
template <char... T>