Skip to content

Instantly share code, notes, and snippets.

@bion
bion / gist:589337697fcb02415e99
Created August 24, 2014 04:02
Javascript context bindng
/*
in functions evaluated 'anonymously', that is without a reference to an object,
the 'this' keyword will reference the global object. in a web page that will end
up being 'Window', in node it will be the node process itself.
'this.number' will be undefined here because the global object has no property called 'number'
*/
var one = function() {
console.log(this.number + ": 'this' referenced inside bare function binds to the global object:", this);
};
#!/bin/bash
portnum=3001
sudo fuser $portnum/tcp 2>1 |
cut -f 1 |
tr -s ' ' '\n' |
sed 1d |
while read -r pid; do
sudo cat /proc/$pid/comm

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@bion
bion / blocks_and_methods.rb
Created January 26, 2015 06:24
Ruby concepts 2
class Foo
def double_then_yield(num, &block)
block.call(num * 2)
end
end
foo = Foo.new
result = foo.double_then_yield 5 do |num|
num + 1
@bion
bion / Makefile
Created February 13, 2015 18:57
Zed's Makefile
CFLAGS=-g -02 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
LIBS=-ldl $(OPTLIBS)
PREFIX?=/usr/local
SOURCES=$(wildcard src/**/*.c src/*.c)
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
@bion
bion / scbeef
Last active August 29, 2015 14:15
scbeef
// should load all the files in a given dir, and then return an
// event (basically a dictionary) with keys as filenames and
// values as whatever the loaded file returned.
// in this case, each file returns a lambda that takes two args
var gestureLibrary = ~dirToEvent.value(~baseDir ++ "/gestures");
// actually returns an empty event because the sclang interpreter
// is easily confused about its pwd
gestureLibrary.postln; // => ( )
@bion
bion / sine_table.c
Last active August 29, 2015 14:20
Sine Table
#include <math.h>
#define PI 3.14159265358979323846
int sinetable(double **table, double frequency, int sample_rate) {
int i = 0;
double phase = 0;
double cycles_per_frame = frequency / sample_rate;
int frames_per_cycle = (int) (1 / cycles_per_frame);
double phase_increment = cycles_per_frame * 2 * PI;
@bion
bion / async_countdown.js
Created May 10, 2015 08:56
async-countdown
var fs = require('fs');
var files = fs.readdirSync("./test");
var everybody = "";
var doneCount = files.length;
var i;
for (i = 0; i < files.length; ++i) {
fs.readFile("./test/" + files[i], function(err, data) {
if (err != null) throw new Error(err);
@bion
bion / cartesian_product.cpp
Last active August 29, 2015 14:23
cartesian product c++
#include <iostream>
#include <deque>
template<typename T>
std::deque< std::deque<T> >&&
cartesian_product(std::deque< std::deque<T> > &bags) {
std::deque< std::deque<T> > output{std::deque<T>{}};
if (!bags.size())
return std::move(output);
#include <iostream>
#include <vector>
template<typename T>
T my_func(typename std::vector<T>::iterator begin) {
return *begin;
}
int main() {
std::vector<int> v{1, 2};