Skip to content

Instantly share code, notes, and snippets.

Avatar

Spence spence

View GitHub Profile
@spence
spence / default-multidimension-array.js
Last active Feb 1, 2020
Messing around with multidimensional arrays in JS.
View default-multidimension-array.js
const createMultidimensionalArray = (dimensions, defaultValue = undefined) => {
if (dimensions < 1) {
throw new Error('Dimensions must be greater than 0');
}
return new Proxy([], {
get(target, prop) {
if (!(prop in target)) {
if (dimensions === 1) {
return defaultValue;
}
View generate_hist.sh
# brew install gnuplot
histfile=histogram-bytes.png
# histfile=histogram-random.png
psql -p5432 -h localhost -d happy_dev -U happy_dev << EOF 2>&1 | head -n -1 | cut -d' ' -f3 | gnuplot -p -e "n=100;max=9999.;min=1000.;width=(max-min)/n;hist(x,width)=width*floor(x/width)+width/2.0;set term png;set output '$histfile';set xrange [min:max];set yrange [0:];set offset graph 0.05,0.05,0.05,0.0;set xtics min,(max-min)/5,max;set boxwidth width*0.9;set style fill solid 0.5;set tics out nomirror;set xlabel 'x';set ylabel 'Frequency';plot '-' u (hist(\$1,width)):(1.0) smooth freq w boxes lc rgb'green' notitle;"
DO
\$do\$
DECLARE
i INT;
BEGIN
View print_1m_ints.sql
DO
$do$
DECLARE
i INT;
BEGIN
FOR i IN 1..1000000 LOOP
SELECT LEFT(('x' || RIGHT(gen_random_bytes(3)::TEXT, 6))::BIT(24)::INT::TEXT, 4)::INT INTO i;
RAISE NOTICE '%', i;
END LOOP;
END
View keybase.md

Keybase proof

I hereby claim:

  • I am spence on github.
  • I am spence (https://keybase.io/spence) on keybase.
  • I have a public key whose fingerprint is 9C16 5E92 0DF1 FA53 B80E AFCB CF19 C3ED D6C4 63B6

To claim this, I am signing this object:

@spence
spence / run.py
Last active Aug 29, 2015
Failing issue with geventwebsocket + newrelic instrumentation. run via `chmod +x run.py && ./run.py` and navigate to http://localhost:9191. After the first websocket connection, no more connections are allowed. Commenting out NR instrumentation restores expected behavior.
View run.py
#!/usr/bin/env python2.7
# pip install newrelic gevent gevent-websocket flask
# my versions: (2.50.0.39) (1.0.2) (0.9.3) (0.10.1)
import newrelic.agent
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer
from flask import Flask, request
# Flask app
@spence
spence / skype-link.html
Created May 29, 2015
Create a emailable link to a Skype profile (since skype has no web profiles and gmail blocks direct application links). e.g., http://static.spencercreasey.com/skype-profile.html
View skype-link.html
@spence
spence / find_anagrams.py
Created May 17, 2015
Given a string of words, find all those that are anagrams.
View find_anagrams.py
def find_anagrams(words):
"""
Find anagrams in O(n*m) time and O(n) space.
Notes: returns all words in input that are anagrams of another word.
"""
anagrams = []
sorted_words = {}
for word in words: # O(n)
View products.py
def get_products_of_all_ints_except_at_index(int_array):
"""
Single pass.
"""
# Condition where element has neither left nor right elements
if len(int_array) == 1:
# Maybe [None] ?
return [0]
View phantomjs-repl.sh
#!/usr/bin/env bash
### Repl Usage:
#
# $ ./phantomjs-repl.sh
# > missing
# ReferenceError: Can't find variable: missing
# > window
# [object Window]
# > console.log('Hello')
@spence
spence / enableautocomplete.js
Created Apr 8, 2014
Enable Autocomplete for sites that disable it (bookmarklet)
View enableautocomplete.js
var forms = document.getElementsByTagName("form");
var inputs = document.getElementsByTagName("input");
var fields = Array.prototype.concat.apply(Array.prototype.slice.call(forms, 0), inputs);
for (var i = 0; i < fields.length; i++) {
fields[i].setAttribute("autocomplete", "on");
fields[i].onpaste = null;
if (window.jQuery) {
window.jQuery(fields[i]).unbind("paste");
}
}