Skip to content

Instantly share code, notes, and snippets.

View michaelfeathers's full-sized avatar

Michael Feathers michaelfeathers

View GitHub Profile
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 / 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
FIELDS_TEXT = "class A; def a; @a = @b; end; end"
FIELDS_SEXP = Ripper.sexp(FIELDS_TEXT)
[:program, [[:class, [:const_ref, [:@const, "A", [1, 6]]], nil,
[:bodystmt, [[:def, [:@ident, "a", [1, 13]], [:params, nil, nil, nil, nil, nil],
[:bodystmt, [[:assign, [:var_field, [:@ivar, "@a", [1, 16]]],
[:var_ref, [:@ivar, "@b", [1, 21]]]]], nil, nil, nil]]], nil, nil, nil]]]]
class Object
def ary?; is_a? Array; end
def str?; is_a? String; end
def nonempty_ary?
ary? && (not empty?)
end
end
def sexp_select sexp, symbols
ENCODER = Hash[('a'..'z').map {|x| [x,x] }].merge(Hash["aeiouya".chars.each_cons(2).to_a])
DECODER = ENCODER.invert
def encode string
string.chars.map {|c| ENCODER[c] || c }.join
end
def decode string
@michaelfeathers
michaelfeathers / gist:4704833
Last active December 12, 2015 03:09
Generating dot files from Ruby code.
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)
def field_names
@field_names ||= @expression.flatten
.each_cons(2)
.select {|marker, _| marker == :@ivar }
.map {|_,name| field_name(name) }
end
Vector vList = new Vector(10);
BufferedReader listFile = new BufferedReader(new FileReader(emailListFile));
String line = null;
while ((line = listFile.readLine()) != null) {
vList.addElement(new InternetAddress(line));
}
listFile.close();
if (ls.debugOn)
log.put(new Date() + "> " + "Found " + vList.size() + " email ids in list");
ls.toList = new InternetAddress[vList.size()];