Skip to content

Instantly share code, notes, and snippets.

@nathanwdavis
nathanwdavis / .robomongorc.js
Created April 30, 2015 14:26
Put this in your .robomongorc.js to get a toCSV() function you can stick on the end of a query to output CSV
//// toCSV
var toCSV = function(data, options){
var headers = [];
var records = [];
var line;
var response = '';
var i, l = data.length, rl=0, j, k;
var reReplaceTextDelim;
var processRecord = function(src, rec, prefix){
@nathanwdavis
nathanwdavis / out
Created March 26, 2015 18:55
Strange behavior from lodash in Node REPL
> _ = require('lodash');
{ [Function: lodash]
support: { funcDecomp: true, funcNames: true },
templateSettings:
{ escape: /<%-([\s\S]+?)%>/g,
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
variable: '',
imports: { _: [Circular] } },
after: [Function: after],
@nathanwdavis
nathanwdavis / rank.go
Last active August 29, 2015 14:14
Build a new rational number from the center point of two previous rationals for each iteration with a pattern like (seeded with 1/2 and 1/1): 3/4, 7/8, 15/16, 31/32, etc
package main
import "fmt"
import "math/big"
func main() {
bound_left := new(big.Rat)
bound_left.SetFloat64(0.5)
bound_right := new(big.Rat)
bound_right.SetFloat64(1.0)
@nathanwdavis
nathanwdavis / gist:8586650
Created January 23, 2014 20:58
What might be blocking my Postgres ALTER?
select distinct st.pid,
current_timestamp - st.query_start as locked_time,
st.state,
st.usename,
st.client_hostname,
st.application_name
from pg_locks l
inner join pg_stat_activity st on l.pid = st.pid
where st.datname = 'analytics_master'
order by current_timestamp - st.query_start desc;
@nathanwdavis
nathanwdavis / gist:b73e291b8cdef993c159
Last active December 25, 2015 07:19
My rebasing help
- ensure latest of master (or rebase source)
- (in dest branch) git rebase -i master
- follow direction, don't forget you need to `git add` changed files
- if there is a conflict
- you can fix it manually
- or if you want to just use HEAD: `git checkout HEAD -- path/to/file.js`
- git push -f origin {branch-name}
@nathanwdavis
nathanwdavis / partitioned_loops.go
Last active December 21, 2015 10:09
Messing around with really CPU intensive math ops and comparing simple for loops vs. goroutine per loop vs. partitioning into multiple goroutines (lightweight, green threads) in Go. The range partitioned goroutine-based solution tends to run around 3.5 - 4x faster than the simple for loop using 8 goroutines. The one that uses a goroutine per loo…
/*
Example Output:
➜ go run partitioned_loops.go
Starting...
simpleFor result: 468767562500, time: 0.013604745000000001 sec
goForEach result: 468767562500, time: 24.106190332 sec
goPartitioned result: 468767562500, time: 0.004296383 sec
*/
package main
@nathanwdavis
nathanwdavis / server.js
Created February 15, 2012 16:57
latency testing node service - just pass in a size param to specify n bytes to reply with. Ex: http://127.0.0.21:7777/?size=5120000 = 5meg response
//defaut settings
var DEFAULT_SIZE = 1000*1024,
DEFAULT_CHUNK_SIZE = 250*1024,
MAX_CHUNK_SIZE = 500*1024;
var http = require('http'),
url = require('url'),
buffer;
@nathanwdavis
nathanwdavis / zeromq_pub.js
Created April 13, 2011 18:03 — forked from timcash/zeromq_pub.js
a more refined version of https://gist.github.com/907514 - a test for publish speed of zeromq in nodejs
var cnt, mes, s, sender, util, zmq, ms;
ms = 1000;
zmq = require('zeromq');
util = require('util');
s = zmq.createSocket('pub');
s.bind('tcp://127.0.0.1:5559', function(err) {
if (err) {
throw err;
@nathanwdavis
nathanwdavis / zeromq_pub.js
Created April 13, 2011 18:02
a more refined version of https://gist.github.com/907514 - a test for publish speed of zeromq in nodejs
var cnt, mes, s, sender, util, zmq, ms;
ms = 1000;
zmq = require('zeromq');
util = require('util');
s = zmq.createSocket('pub');
s.bind('tcp://127.0.0.1:5559', function(err) {
if (err) {
throw err;