Skip to content

Instantly share code, notes, and snippets.

View mdunsmuir's full-sized avatar

Michael Dunsmuir mdunsmuir

  • Seattle
View GitHub Profile
@mdunsmuir
mdunsmuir / mdop.rb
Created October 17, 2012 22:59
ruby alternative option parser
#
# simple option parser that supports negative numbers as arguments, because I needed to do that
#
# should mostly be a drop-in replacement for OptionParser's basic usage patterns
#
class MDOP
#
# class to hold each option's data (including its Proc)
#
tape = {}
tape.default = 0
ptr = 0
tape[ptr] += 13
while tape[ptr] != 0
tape[ptr] -= 1
tape[ptr + 1] += 2
tape[ptr + 4] += 5
tape[ptr + 5] += 2
tape[ptr + 6] += 1
@mdunsmuir
mdunsmuir / find_neighbors_hash.rb
Created May 6, 2013 22:14
Hash subclass for Numeric keys only, returns either the actual value for an existing key, the values for the lower and upper *bounding* keys respectively for a bounded unknown key, or the value for the greatest or smallest existing key in the case of an out-of-bounds unknown key.
#
# This subclass of Hash is designed to search for the values associated
# with the two nearest Numeric keys to a given "key" argument *not*
# a member of the FindNeighborsHash, or the *single* value associated with
# an *existing* key.
#
class FindNeighborsHash < Hash
def []=(key, val)
raise ArgumentE
filetype indent on " automatically indent
set expandtab " tabs are spaces
set shiftwidth=2 " two space indent
set tabstop=2
set ruler " display line number and position
"This unsets the "last search pattern" register by hitting return
nnoremap <CR> :noh<CR><CR>
@mdunsmuir
mdunsmuir / randomtext.rb
Created June 4, 2013 07:30
was bored, so random text generator (don't run james joyce's dirty letters through it)
# parse input
input = IO.read(ARGV.shift)
scanned = input.scan(/([A-Z].+?)([.?!])/).collect{ |arr| arr.join(" ").split }
words = Hash.new # store the successive probabilities of words
firstcount = 0
firsthash = Hash.new(0) # store the initial words' probabilities
scanned.each do |sentence|
last = sentence.shift
@mdunsmuir
mdunsmuir / email_to_me.rb
Created October 21, 2013 19:28
handy script for emailing files to yourself from places where other means of transfer are unavailable.
require 'mail'
require 'iconv'
file, subj, email = *ARGV
raise "usage: #{File.basename(__FILE__)} <file> <subject> <email>" unless email
file_content = IO.read(file)
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
@mdunsmuir
mdunsmuir / maximum_path_sum.hs
Created December 9, 2013 19:15
Haskell I/O sample program (Euler 18)
import System.Environment(getArgs)
main = do args <- getArgs
if length args > 0
then do inpstr <- readFile $ head args
putStrLn $ show (maxPathSum (parseData inpstr))
else putStrLn "I need a filename with triangle data!"
parseData s = map (\s -> map read (words s)) $ lines s
@mdunsmuir
mdunsmuir / threaded_systems.rb
Created December 20, 2013 01:01
simple, stupid thread pool for running system calls
MAX_THREADS = 4
def self.run_threaded_systems(tasks)
queued_tasks = tasks.each_with_object(Queue.new) do |task, queue|
queue.push(task)
end
MAX_THREADS.times.inject(Array.new) { |threads|
threads << Thread.new(queued_tasks) { |queue|
while task = queued_tasks.pop(true) rescue nil
puts("running command #{task.join(" ")}")
WIDTH = 80
puts $stdin.read.split(/\n\n+/).each_with_object([]) { |paragraph, result|
result << paragraph.split.each_with_object([""]) { |word, lines|
if (lines.last + " " + word).length > WIDTH
lines << word
elsif lines.last.empty?
lines[-1] += word
else
lines[-1] += (" " + word)
require 'thread'
module KnifeDrawer
DEFAULT_NUM_FORKS = 4
def self.do_with_forks(tasks_hash, num_forks = DEFAULT_NUM_FORKS)
input_queue = Queue.new
output_queue = Queue.new