Skip to content

Instantly share code, notes, and snippets.

@pianosnake
pianosnake / binary_heaps.js
Created January 8, 2013 07:13
Extensions to JavaScript arrays to provide binary heap operations on objects with an integer f-property like {f: 10}
if(typeof Array.prototype.pushHeap !== "function"){
Array.prototype.pushHeap = function(v){
//add item to the end. this is supposedly faster than pushing http://dev.opera.com/articles/view/efficient-javascript/?page=all
this[this.length]=v;
//update the index of v to show it's at the end of the array
this[this.length-1].i=this.length-1;
var h= Math.floor(this.length/2),
p = this[h-1],
vpos = this.length-1;
@pianosnake
pianosnake / gist:6478532
Last active December 22, 2015 13:19
JavaScript class pattern for RubyMine live template
function $CLASS$() {
$END$
}
;(function(proto){
proto.method = function(){
}
@pianosnake
pianosnake / gist:6481667
Created September 8, 2013 03:46
RubyMine live template for Jasmine test
describe("$CLASS$", function(){
it("should $END$", function(){
});
});
@pianosnake
pianosnake / drawVexNote.js
Last active December 27, 2015 13:09
This is the code it takes to display one note using the JS VexFlow library (http://www.vexflow.com/) Takes a note with an octave and a name like 'g#'
function drawVexNote(note){
var renderer = new Vex.Flow.Renderer("note-canvas", Vex.Flow.Renderer.Backends.CANVAS);
var ctx = renderer.getContext();
ctx.clear();
var stave = new Vex.Flow.Stave(0, 0, 100);
stave.addClef("treble").setContext(ctx).draw();
var vexNote = new Vex.Flow.StaveNote({ keys: [note.name+"/"+note.octave], duration: "w" })
@pianosnake
pianosnake / gist:7654006
Last active December 29, 2015 09:59
User Key Bindings for Sublime 2 with RubyMine shortcuts used by Pivotal Labs.
[
{ "keys": ["super+d"], "command": "duplicate_line" },
{ "keys": ["super+y"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Line.sublime-macro"} },
{ "keys": ["super+alt+up"], "command": "swap_line_up" },
{ "keys": ["super+alt+down"], "command": "swap_line_down" }
]
@pianosnake
pianosnake / listen.rb
Created December 4, 2013 05:54
Basic Ruby script to listen for Postgres notifications from a central database and update a client database.
require 'rubygems'
require 'sequel'
require 'json'
require 'pg'
DB_central = Sequel.connect('postgres://xxx:xxx@hostname/central_db')
DB_client1 = Sequel.connect('postgres://xxx:xxx@hostname/client1_db')
DB_central.listen :usersupdate, {loop: true} do |channel, pid, payload|
@pianosnake
pianosnake / trigger_and_function.sql
Last active March 1, 2018 22:39
Function to send a Postgres Notification with a JSON object payload of the updated fields. Also the trigger to call the function.
CREATE OR REPLACE FUNCTION send_notification() RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('usersupdate',
(SELECT row_to_json(r.*)::varchar FROM (
SELECT id, email, name from users where id = NEW.id)
r)
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
@pianosnake
pianosnake / mongo_date.js
Created January 13, 2014 22:25
Get date from Mongo ID
function mongoDate(mongoId){
var timehex = mongoId.substring(0,8);
var secondsSinceEpoch = parseInt(timehex, 16);
return new Date(secondsSinceEpoch * 1000);
}
@pianosnake
pianosnake / jenkins_external_monitor_timeout.sh
Created February 4, 2014 21:12
If you're using a Jenkins external monitoring job, you may want to fail the job if it's been too long since the last run. Put this script in a cron tab to check the time since the last successful build. If it's been longer than 2 minutes it will submit a failed job to Jenkins.
#!/bin/bash
# get the last sucessful build
XML=`curl http://localhost:8080/job/External-Monitoring/lastSuccessfulBuild/api/xml`
# extract the first 10 digits of the timestamp
TIMESTAMP=`echo $XML| sed -E 's/.*timestamp>(.*)\<\/timestamp.*/\1/' | cut -c1-10`
NOW=`date +"%s"`
@pianosnake
pianosnake / arduino-robot-drummer.ino
Last active August 29, 2015 14:06
Arduino UNO code that listens for MIDI signals on the serial port (RX). A snare and bass drum are struck by 12V solenoid motors connected to ports 8, 9 and 10. If the MIDI signal calls for a repeat snare strike quicker than 100ms, the second snare motor is used to give the first motor time to recover. If the pedal on port 4 is pressed, the MIDI …
#include <MIDI.h>
byte pedal = 4;
byte bassDrum = 8;
byte snareDrum1 = 9;
byte snareDrum2 = 10;
byte snareAlternate = true;
byte bassDrumPitch = 36;
byte snareDrumPitch = 38;