Skip to content

Instantly share code, notes, and snippets.

@eihli
eihli / tcp.c
Created January 6, 2022 05:29
Casting a byte array to int, long, etc...
#include <stdio.h>
#include <stdint.h>
int main() {
// Mimic reading that TCP header file 1 byte at a time into a buffer.
// The first 2 bytes are the source port.
// In this case, port 257.
unsigned char buffer[2] = { 0x01, 0x01 }; // 0000001 00000001 = 257
// Now convert those two bytes into a single int, since ports are 16 bits.
@eihli
eihli / weighted_rand.clj
Created October 23, 2020 18:29 — forked from ghadishayban/weighted_rand.clj
Vose's alias method for weighted randoms
(ns weighted-rand
(:import clojure.lang.PersistentQueue))
(defprotocol Rand
(nextr [_ rng]))
;; Vose's alias method
;; http://www.keithschwarz.com/darts-dice-coins/
(deftype Vose [n ^ints alias ^doubles prob]

Keybase proof

I hereby claim:

  • I am eihli on github.
  • I am eihli (https://keybase.io/eihli) on keybase.
  • I have a public key whose fingerprint is ED9F 8D1F A0DD 191A 9844 F72F A2FF 66D6 1D56 E0B5

To claim this, I am signing this object:

@eihli
eihli / retriable_example.rb
Last active October 15, 2016 19:03
Composable Retriable in Ruby
def attempt_to_deliver
rand(10) > 8
end
class Deliverable
def deliver
if attempt_to_deliver
puts "Delivered"
true
else
import os
from circus.plugins import CircusPlugin
from circus import logger
from zmq.eventloop import ioloop
import time
import pdb
class CircusWatcher(CircusPlugin):
@eihli
eihli / onchange.sh
Created September 5, 2016 16:57 — forked from senko/onchange.sh
Watch current directory and execute a command if anything in it changes
#!/bin/bash
#
# Watch current directory (recursively) for file changes, and execute
# a command when a file or directory is created, modified or deleted.
#
# Written by: Senko Rasic <senko.rasic@dobarkod.hr>
#
# Requires Linux, bash and inotifywait (from inotify-tools package).
#
# To avoid executing the command multiple times when a sequence of
@eihli
eihli / __new__ question
Created September 28, 2014 21:13
Use of __new__
class Game (object):
"""
Game represents a single pre- or regular-season game. It provides a window
into the statistics of every player that played into the game, along with
the winner of the game, the score and a list of all the scoring plays.
"""
def __new__(cls, eid=None, fpath=None):
# If we can't get a valid JSON data, exit out and return None.
try: