Skip to content

Instantly share code, notes, and snippets.

View btmills's full-sized avatar
🐃
Who knew yaks were so hairy?

Brandon Mills btmills

🐃
Who knew yaks were so hairy?
View GitHub Profile
@btmills
btmills / throttle.js
Created January 20, 2016 22:58
Throttle function
const Throttle = (delay = 30) => {
let timer = null;
let start = null;
return (cb) => {
if (start) {
const remaining = delay - (Date.now() - start);
clearTimeout(timer);
timer = setTimeout(() => {
@btmills
btmills / kinda-redux.js
Created January 20, 2016 22:57
Simple example of state, actions, and reducers
const INCREMENT = Symbol('INCREMENT');
const DECREMENT = Symbol('DECREMENT');
const makeReducer = (initialState, reducers = {}, props = {}) =>
(state = initialState, action) =>
reducers.hasOwnProperty(action.type) ? reducers[action.type](state, action) : state;
const reducer = makeReducer(42, {
[INCREMENT]: (state, action) => state + 1,
[DECREMENT]: (state, action) => state - 1

Keybase proof

I hereby claim:

  • I am btmills on github.
  • I am brandon (https://keybase.io/brandon) on keybase.
  • I have a public key whose fingerprint is EE6D 75A3 23A7 208A CC35 9A65 7D5F D8A7 2009 AC0D

To claim this, I am signing this object:

@btmills
btmills / id.coffee
Created October 30, 2013 23:45
Generate a random string id given a set of characters and a desired length
id = (chars, len) ->
(chars[Math.ceil(Math.random()*chars.length)-1] for i in [1..len]).join('')
@btmills
btmills / globalize.coffee
Created August 10, 2013 19:09
Import an object's members into the global namespace. Here Be Footguns.
globalize = (obj) ->
global[k] = v for k, v of obj
@btmills
btmills / autopoke.js
Created July 9, 2013 08:33
Run this while on https://www.facebook.com/pokes to beat an opponent into submission.
var dashboard = document.getElementsByClassName('pokesDashboard')[0];
window.setInterval(function () {
var pokes = dashboard.getElementsByClassName('uiIconText');
for (var i = 0; i < pokes.length; i++)
pokes[i].click();
}, 1000);
@btmills
btmills / insert_pool.js
Last active July 22, 2019 00:52
Use a connection pool to rapidly insert a large number of records into a RethinkDB database.
var r = require('rethinkdb');
// How many connections in the pool?
// 2547 will fail with "(libuv) Failed to create kqueue (24)"
var CONNECTIONS = 2546;
// Insert this many rows
var COUNT = 10000;
// Database to use for the test
var DB = 'test'
// Table to use for the test
@btmills
btmills / insert.js
Created March 23, 2013 23:26
Rapidly insert a large number of records in a RethinkDB database. Noticing failure for large number of inserts.
var r = require('rethinkdb');
// Insert this many rows
// 2547 will fail on connection with
// "(libuv) Failed to create kqueue (24)"
var COUNT = 2546;
// Database to use for the test
var DB = 'test'
// Table to use for the test
var TABLE = 'insertTest';
var spellings = ['(E|e)nt', ['rep', 'ep'], ['ren', 'en'], ['eur', 'er']];
//var endings = ['', 'ship', 'ial'];
var parts = [];
for (var i = 0; i < spellings.length; i++) {
if (spellings[i] instanceof Array) {
parts.push('(?:');
for (var j = 0; j < spellings[i].length; j++) {
parts.push(spellings[i][j]);
@btmills
btmills / ufgets.c
Last active December 13, 2022 01:44
Unlimited fgets() - read input from the console with automatic buffer sizing.
#include <stdio.h>
#include <stdlib.h>
/*
* Similar to fgets(), but handles automatic reallocation of the buffer.
* Only parameter is the input stream.
* Return value is a string. Don't forget to free it.
*/
char* ufgets(FILE* stream)
{