Skip to content

Instantly share code, notes, and snippets.

View michaelfeathers's full-sized avatar

Michael Feathers michaelfeathers

View GitHub Profile
@michaelfeathers
michaelfeathers / gist:1e2934ebb95560cddc3b
Created April 3, 2015 02:05
return a string that represents increases, decreases, and stable changes in a method's length
# :: [event] -> String -> String
def method_delta_line es, method_name
es.select {|e| e.method_name == method_name }
.map(&:method_length)
.each_cons(2)
.map {|c,n| ["^","v","-"][(c <=> n) + 1] }
.join
end
require 'ap'
require 'ripper'
CLASS_TEXT = "class A; end; class B; end"
CLASS_SEXP = Ripper.sexp(CLASS_TEXT)
BIG_TEXT = "class A; def a; @a = b; end; def b; @d = a; @e = a; end; end; module B; def b; end; end"
BIG_SEXP = Ripper.sexp(BIG_TEXT)
STRING_COUNT = 6
def tab_column string, fret
["---" ] * (string - 1) +
[fret.ljust(3,'-')] +
["---" ] * (STRING_COUNT - string)
end
puts ARGF.each_line
.map(&:split)
stems = ARGF.read
.split
.each_cons(2)
.group_by { |word_pair| word_pair[0] }
def next_word ary
ary[rand(ary.length).to_i][1]
end
e = Enumerator.new do |e|
@michaelfeathers
michaelfeathers / gist:56f34287dbb55dc0d4a2
Last active August 29, 2015 14:02
An example of Literate Chaining Style in Ruby. The Kernel#c method is a pass through that serves as a placeholder for a comment and a convenient place for tracing.
# Accepts program file names on the command line and renders
# their text to stdout as a sequence of spoken words
token_map = {
"[" => "left square-bracket",
"]" => "right square-bracket",
"!" => "exclamation-point",
"\\" => "back slash",
"#" => "pound sign",
"\"" => "double-quote",
@michaelfeathers
michaelfeathers / gist:242d0ad0baa57765ced5
Created June 7, 2014 19:46
Literate Chaining Style in Ruby
module Kernel; def c text; self; end; end
puts ARGF.c("all files as text") .read
.c("convert to enumerable") .chars
.c("contiguous alphas and puncts") .chunk {|ch| ch =~ /[[:alnum:]]/ ? :alone : true }
.c("merging identifiers and nums") .map {|key,values| key == :alone ? values.join : values }
.c("array of idents and puncts") .flatten(1)
@michaelfeathers
michaelfeathers / gist:f52c48e2455a75374dc8
Created June 7, 2014 19:12
Audiobook scripting utility
# Accepts program file names on the command line and renders them to stdout as a sequence of spoken words
token_map = {
"[" => "left-square-bracket",
"]" => "right-square-bracket",
"!" => "exclamation-point",
"\\" => "backslash",
"#" => "pound sign",
"\"" => "double-quote",
@michaelfeathers
michaelfeathers / saddle.hs
Created May 15, 2013 14:11
Create a zip-esque running annotation for a list based upon a predicate.
saddle :: (a->Bool) -> [a] -> [(a,a)]
saddle f [] = []
saddle f (x:xs) = saddle' f x xs
saddle' :: (a->Bool) -> a -> [a] -> [(a,a)]
saddle' f initial [] = []
saddle' f initial (x:xs) = (x, sideValue x) : saddle' f (sideValue x) xs
where sideValue x = if f x then x else initial
@michaelfeathers
michaelfeathers / dentdetect.rb
Last active December 16, 2015 07:08
Attempt to discern number of spaces used for an indent across a set of source files.
class Array
def freq_by &block
group_by(&block).map {|k,v| [k, v.count] }.sort_by(&:first)
end
def freq
freq_by {|e| e }
end
end
@michaelfeathers
michaelfeathers / gist:5378227
Last active December 16, 2015 04:38
Report leading whitespace in a set of files to characterize programmers' indentation conventions.
class Array
def freq_by &block
group_by(&block).map {|k,v| [k, v.count] }.sort_by(&:first)
end
def freq
freq_by {|e| e }
end
end