Saturday, January 4, 2014

Using ruby to explore scalaz

I've been poking around scalaz, and sometimes it's nice to just get a quick overview of the code using some simple ruby. I could write some code using asm to look at the bytecode, but it's usually faster for me to just use ruby on the source. I'm not looking for 100%-complete tested code here, it's more like a very fancy grep.

For example, I was curious about all the places that define functions starting with lift:

require 'pp'
ARGF.each_line do |line|
next unless line =~ /def lift/
# def liftFnord[asdf]9) =
elements = / def lift(.*)=*/
next if line =~ /lift[2-9]/
next if line =~ /lift\d\d/
the_function = "lift" + line.match(elements)[1]
fn_name, _ = the_function.split(/[\[(]/,2)
fname = File.basename ARGF.filename
puts "#{fname}\t#{fn_name}\t#{the_function}\t#{ARGF.lineno}\t#{line}"
end
view raw lift.rb hosted with ❤ by GitHub


And then to read the output, I use a google spreadsheet:



Run with:

find * -name *.scala -type f -print0 | xargs -0 ruby lift.rb | pbcopy

pbcopy is the mac utility that copies stdout to the clipboard, so it's fast to run this from the command line, switch to the spreadsheet, and hit paste.