Skip to content

Instantly share code, notes, and snippets.

@codeslinger
codeslinger / weighted_random_sampling.md
Created September 8, 2017 18:58 — forked from O-I/weighted_random_sampling.md
[TIx 8] Weighted Random Sampling in Ruby

One of the many reasons I love working with Ruby is it has a rich vocabulary that allows you to accomplish your goals with a minimal amount of code. If there isn't a method that does exactly what you want, it's usually possible to build an elegant solution yourself.

Let's take the example of simulating the rolling of a die.

We can represent a die as an array of its faces.

die = [*?⚀..?⚅]
# => ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]
@codeslinger
codeslinger / variants.c
Created June 26, 2017 13:46
Tagged unions (a.k.a variants) in C
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#define var __auto_type
#define let __auto_type const
static inline void * variant_cast(void * variant_ptr, ptrdiff_t desired_tag) {
ptrdiff_t * variant_tag = (ptrdiff_t *)variant_ptr;
assert(*variant_tag == desired_tag);
@codeslinger
codeslinger / x.py
Last active April 20, 2017 18:04
Handy Python functions
# Applies functions in `fs` in order whose input starts with `iv` and
# each function's output is fed into the next function in the chain.
# Should any function return `None`, the chain is broken, execution
# stops and `None` is returned.
def chain(iv, *fs):
v = iv
for f in fs:
v = f(v)
if v is None:
break
#!/usr/bin/env python2.7
from __future__ import print_function
import commands
import os
import stat
from gitlab import Gitlab
def get_clone_commands(token, repo_root):
con = Gitlab("http://gitlab.your.domain", token)
@codeslinger
codeslinger / reclaimWindows10.ps1
Created January 9, 2017 00:40 — forked from alirobe/reclaimWindows10.ps1
"Reclaim Windows 10" turns off a bunch of unnecessary Windows 10 telemetery, removes bloatware, and privacy invasions. Review and tweak before running. Scripts for reversing are included and commented. Fork via https://github.com/Disassembler0 (different defaults)
##########
# Win10 Initial Setup Script
# Author: Disassembler <disassembler@dasm.cz>
# Version: 1.7, 2016-08-15
# dasm's script: https://github.com/Disassembler0/Win10-Initial-Setup-Script/
# THIS IS A PERSONALIZED VERSION
# This script leaves more MS defaults on, including MS security features.
# Tweaked based on personal preferences for @alirobe 2016-11-16 - v1.7.1
# basic pfctl control
# ==
# Related: http://www.OpenBSD.org
# Last update: Tue Dec 28, 2004
# ==
# Note:
# this document is only provided as a basic overview
# for some common pfctl commands and is by no means
# a replacement for the pfctl and pf manual pages.
@codeslinger
codeslinger / freebsd-qemu-xhyve-mac-os-x-virtual-machine.md
Created May 14, 2016 01:03 — forked from zg/freebsd-qemu-xhyve-mac-os-x-virtual-machine.md
Create FreeBSD virtual machine using qemu. Run the VM using xhyve.

TL;DR

  • Create 10GB FreeBSD image using QEMU.
  • Run the VM using xhyve.
  • Mount host directory.
  • Resize the image.

Requisites

EAL: Detected lcore 0 as core 0 on socket 0
EAL: Detected lcore 1 as core 1 on socket 0
EAL: Detected lcore 2 as core 2 on socket 0
EAL: Detected lcore 3 as core 3 on socket 0
EAL: Detected lcore 4 as core 4 on socket 0
EAL: Detected lcore 5 as core 5 on socket 0
EAL: Detected lcore 6 as core 6 on socket 0
EAL: Detected lcore 7 as core 7 on socket 0
EAL: Detected lcore 8 as core 8 on socket 0
EAL: Detected lcore 9 as core 0 on socket 1
os_http_be.map and os_sni_passthrough.map are both just empty files
@codeslinger
codeslinger / gist:6390932
Created August 30, 2013 15:18
Makefile for Go projects
# vim:set ts=8 ai:
GOPATH := $(shell pwd)
BASE := github.com/me/myproject
TARGETS := target1 target2 target3
PACKAGES := util1 util2 target1 target2 target3
GOTESTOPTS := -v
GOGETOPTS := -v
all: compile