Skip to content

Instantly share code, notes, and snippets.

View bgreenlee's full-sized avatar

Brad Greenlee bgreenlee

View GitHub Profile
@bgreenlee
bgreenlee / multipart_binary_posts.js
Created September 17, 2011 00:07
Multipart Binary POSTs in Javascript #javascript
var dataURL = this.canvas.toDataURL(this.getImageType()); // grab the snapshot as base64
var imgData = atob(dataURL.substring(13 + this.getImageType().length)); // convert to binary
var filenameTimestamp = (new Date().getTime());
var separator = "----------12345-multipart-boundary-" + filenameTimestamp;
// Javascript munges binary data when it undergoes string operations (such as concatenation), so we need
// to jump through a bunch of hoops with streams to make sure that doesn't happen
// create a string input stream with the form preamble
@bgreenlee
bgreenlee / @
Last active July 21, 2016 14:37
#!/bin/bash
# "@Pad"
# An easy commandline time-stamped log/notepad
# Derived from https://web.archive.org/web/20120118122636/http://blog.rubybestpractices.com/posts/jamesbritt/James_will_be_right_back_after_these_interruptions.html
#
# Usage:
# @ something or other - log the timestamped message "something or other"
# @ . - open the @ scratchpad with a new timestamp and
# no message with your default editor
{ "keys": ["super+f4"], "command": "public_gist_from_selection" }
@bgreenlee
bgreenlee / gist:5305273
Last active December 15, 2015 18:39
Coalesce a list of overlapping start/end tuples
val ranges = List((2,3), (1,2), (5,11), (4,10), (3,3), (6,7), (15,16))
ranges.sorted.foldLeft(List[(Int, Int)]()) { (acc, t) =>
acc match {
case x :: xs if x._2 >= t._1 => (x._1, math.max(x._2, t._2)) :: xs
case x :: xs => t +: acc
case _ => List(t)
}
}.reverse
@bgreenlee
bgreenlee / crontab
Last active December 11, 2015 19:59
Poor man's Nagios
# logtail from logcheck (http://logcheck.org/)
*/15 * * * * /usr/sbin/logtail /path/to/some/log/file.log | egrep "WARN|ERROR|Exception" | head
@bgreenlee
bgreenlee / sieve.scala
Created November 9, 2012 17:40
Sieve of Eratosthenes in Scala #scala #algorithms
# sieve of eratosthenes
def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0))
def from(n: Int): Stream[Int] = n #:: from(n + 1)
def primes = sieve(from(2))
@bgreenlee
bgreenlee / stackscript.sh
Created August 13, 2012 04:32
Linode Chef Bootstrap StackScript #linode #chef
#!/bin/bash
# <udf name="hostname" label="Hostname">
# <udf name="ssh_key" label="SSH Key">
# <udf name="ubuntu_mirror" label="Ubuntu Mirror URL" default="http://us.archive.ubuntu.com/ubuntu">
# <udf name="distro" label="Ubuntu Distro" default="maverick">
# <udf name="default_ruby_interpreter" label="Default Ruby Interpreter"
# oneOf="ruby-1.8.6,ruby-1.8.7,ruby-1.9.1,ruby-1.9.2,ruby-head"
# default="ruby-1.9.2">
@bgreenlee
bgreenlee / DefaultKeyBinding.dict
Created February 28, 2012 14:53 — forked from indirect/DefaultKeyBinding.dict
Type unicode arrows and other cool things #unicode #osx
/* Put this in ~/Library/KeyBindings/DefaultKeyBinding.dict */
/* Based on a file from @eventualbuddha */
{
/* Modifier keys: start with C-m */
"^m" = {
"^ " = ("insertText:", "\U2423"); /* C-space space */
"^e" = ("insertText:", "\U21A9"); /* C-e return */
"e" = ("insertText:", "\U2305"); /* e enter */
@bgreenlee
bgreenlee / gist:1328254
Last active September 27, 2015 20:38
Words with 6 consecutive consonants #words
$ egrep -i "[bcdfghjklmnpqrstvwxz]{6,}" /usr/share/dict/words
archchronicler
bergschrund
Eschscholtzia
fruchtschiefer
latchstring
lengthsman
Nachschlag
postphthisic
veldtschoen
@bgreenlee
bgreenlee / version_string.rb
Created September 13, 2011 22:36
Compare two version strings. #ruby
class VersionString < String
def <=>(other)
my_parts = self.split('.').map(&:to_i)
other_parts = other.split('.').map(&:to_i)
my_parts.each_with_index do |part, i|
break if i >= other_parts.length
next if part == other_parts[i]
return part <=> other_parts[i]
end
if my_parts.length > other_parts.length