Skip to content

Instantly share code, notes, and snippets.

@dhoss
dhoss / benchmark-output.txt
Last active August 29, 2015 14:02
with recursive and with recursive inside select .. in (...)
devin@predaking:~/projects/web/treeify$ ruby bin/bm_in_query_vs_execute.rb
-- drop_table(:nodes)
-> 0.0163s
-- create_table(:nodes)
-> 0.0058s
-- add_index(:nodes, [:parent_id, :id], {:unique=>true})
-> 0.0037s
user system total real
in 0.000000 0.000000 0.000000 ( 0.000090)
# Give it two procs to compare
def bm(o, n)
reload!
puts "Running 10 times"
Benchmark.bmbm(10) do |x|
x.report('Old Query:') { @olr = o.call }
x.report('New Query:') { @nr = n.call }
end
reload!
@dhoss
dhoss / search.rb
Created December 9, 2013 18:44
simple ruby search
terms = Hash.new{|h,k|h[k]=h.size}
docs = DATA.collect { |line|
name = line.match(/^\d+/)
words = line.downcase.scan(/[a-z]+/)
vector = []
words.each { |word| vector[terms[word]] = 1 }
{:name=>name,:vector=>vector}
}
current = docs.first # or any other
docs.sort_by { |doc|
@dhoss
dhoss / fib.go
Created November 18, 2013 18:56
Tour of Go (http://tour.golang.org) fibonacci with closures
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func(int) int {
fib := make([]int, 100)
fib[0] = 0
fib[1] = 1
@dhoss
dhoss / wc.go
Created November 18, 2013 18:26
Tour of Go (http://tour.golang.org) word count with maps solution
package main
import (
"strings"
"code.google.com/p/go-tour/wc"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
wordCount := make(map[string]int)
@dhoss
dhoss / initializer.rb
Created September 24, 2013 17:18
carrierwave
CarrierWave.configure do |config|
#if Rails.env.test? or Rails.env.rspec?
# CarrierWave.configure do |config|
# config.storage = :file
# end
#end
config.fog_credentials = {
:provider => 'AWS', # required
@dhoss
dhoss / validate-links.rb
Created July 12, 2013 20:33
ruby validate-links.rb /mnt/hgfs/daustin/work/linkQA.csv validate-links.rb:6:in `<main>': undefined local variable or method `hit_links' for main:Object (NameError)
def grouped_impression_data
raw_impression_data
@impression_data.group_by {|i| i.first}.each {|k,v|
v.each {|val|
val.shift
}
v.flatten
}
end
@dhoss
dhoss / impression_data.rb
Created June 26, 2013 19:04
simple web scraper
#!/usr/bin/env ruby
require 'rubygems'
require 'roo'
require 'csv'
require 'mechanize'
require 'date'
# 1. Read in excel file
# 2. Parse and grab columns of data needed
# 3. Push into an array of URLs, with an associated campaign id
@dhoss
dhoss / Thumbnailer.scala
Created May 2, 2013 19:36
[error] /Users/daustin/web-dev/lumos-pottery/app/thumbnailer/Thumbnailer.scala:41: value scaledWidth is not a member of Int [error] g2d.drawImage(originalImage, 0, 0 scaledWidth, scaledHeight, null)
/** Thumbnailer
* generate a thumbnail of an image
*
* ==Overview==
* {{{
* val thumbnail = new Thumbnailer(.25, "image.jpg", "image-thumbnail.jpg")
* val resized = thumbnail.resize()
* thumbnail.save()
* }}}
*/