Skip to content

Instantly share code, notes, and snippets.

@jmdeldin
jmdeldin / lazy.rb
Created December 15, 2014 22:55
ruby laziness
require 'benchmark/ips'
START = 1
FINISH = 1_000_000
HALF = FINISH / 2
Benchmark.ips do |x|
x.report('lazy') do
(START..FINISH).lazy.include?(HALF)
end
# Pointless implementation of cal(1).
#
# Author :: Jon-Michael Deldin <[email protected]>
# Date :: 2011-06-27
#
class Calendar
# width of calendar in spaces
WIDTH = 20
require 'benchmark/ips'
Cart = Struct.new(:items)
N = 10_000
query = -> { Array.new(N).map { |a| Cart.new(%w(foo bar baz)) } }
Benchmark.ips do |x|
x.report('each') do
@jmdeldin
jmdeldin / uppercase-sql.el
Created October 9, 2014 19:50
Uppercase MySQL keywords in Emacs
(defun jm/fix-sql-case (start end)
"Uppercase SQL keywords"
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char 1)
(let ((case-fold-search nil))
(while (search-forward-regexp
(mapconcat 'downcase (mapcar 'symbol-name '(select from inner outer left join where and in group by into on)) "\\|")
nil t)
@jmdeldin
jmdeldin / gist:11243416
Created April 24, 2014 06:15
Profiling
% gem install perftools.rb
% CPUPROFILE_REALTIME=1 CPUPROFILE_METHODS=1 CPUPROFILE=/tmp/profile RUBYOPT="-r`gem which perftools | tail -1`" ruby /tmp/z.rb && pprof.rb --pdf /tmp/profile > /tmp/profile.pdf
@jmdeldin
jmdeldin / oldies
Created December 14, 2013 01:01
Script for finding old code.
#! /usr/bin/env ruby
#
# Find really old code.
#
# Author: Jon-Michael Deldin
# Date: 2013-12-13
#
# Usage:
# oldies [DIR]
#
@jmdeldin
jmdeldin / compressed.js
Last active December 17, 2015 15:08
Create a bookmark called "Sort Honeybadger" (or something), paste the contents of compressed.js into the location, then run this on one of the Honeybadger error pages.
javascript:(function()%7Bvar table%3D%24(%27.faults%27)%3Bvar f%3Dfunction(tr)%7Breturn parseInt(%24(tr).find(%27.count%27).text().trim())%3B%7D%3Bvar thead%3Dtable.find(%27tr:first%27)%3Bthead.remove()%3Btable.html(table.find(%27tr%27).sort(function(x,y)%7Breturn f(x)<f(y)%3F1:f(x)>f(y)%3F-1:0%3B%7D))%3Btable.prepend(thead)%3B%7D)()%3B
@jmdeldin
jmdeldin / gist:5572953
Created May 14, 2013 01:29
Spell check all Markdown files
aspell list < $(find . -name '*.md') | sort | tr '[A-Z]' '[a-z]' | uniq -c | sort -rn
## Jon-Michael Deldin
## Pattern Recognition
## Edge Detection
## Spring 2013
##
##
## USAGE
## ------
## Rscript edges.R
##
@jmdeldin
jmdeldin / results
Created April 12, 2013 06:41
Benchmarking using semi-unnatural Array methods (#rotate and #drop) against the more natural Set difference.
Rehearsal ----------------------------------------------------------------
array: rotating and dropping 0.050000 0.000000 0.050000 ( 0.055791)
set: difference 8.480000 0.020000 8.500000 ( 8.500697)
set: dup and delete 8.890000 0.020000 8.910000 ( 8.908871)
------------------------------------------------------ total: 17.460000sec
user system total real
array: rotating and dropping 0.060000 0.000000 0.060000 ( 0.056425)
set: difference 8.830000 0.010000 8.840000 ( 8.842558)
set: dup and delete 8.940000 0.030000 8.970000 ( 8.969234)