SLIME in Vim

(7 months ago)

I've been looking around for a way to have similar basic functionality in Vim as you have with Emacs + SLIME. That is, for all the extra goodies in SLIME, I just want an editing buffer alongside a REPL buffer where executing code and retrieving history is easy. Copying and pasting is annoying, as are REPLs with limited editing functionality (like MIT Scheme's). This requires a decent number of tools, but they're all useful enough that you should probably have them anyway:

brew install git macvim rlwrap screen

(And if you have some other *nix, I'll assume you're smart enough to translate the above. And Windows = ¯\_(ツ)_/¯)

In Vim I use Janus a collection of useful plugins and reasonable defaults. The prerequisites are here if your OS doesn't ship with ruby rake, ctags and ack

Janus provides the ~/.janus folder to add your own plugins, so from there it's just a matter of adding the excellent vim-slime plugin:

cd ~/.janus
git submodule add https://github.com/jpalardy/vim-slime

Among other things, this will add some SLIME keybindings to Vim, like C-c, C-c for executing the current paragraph in the REPL buffer. vim-slime defaults to using GNU screen, although there are other options, like tmux. It will prompt you for the name of the session and window unless you specify them explicitly, so I use repl for both.

In my .vimrc.after:

let g:slime_paste_file = "$HOME/.slime_paste"
let g:slime_default_config = {"sessionname": "repl", "windowname": "repl"}

In my .zshrc:

alias slimy='screen -S repl -p repl'

This gives you broad flexibility to send code to any interactive utility. For those with readline support, like irb, you just create a new screen session via slimy irb. For stuff like Scheme, I use slimy rlwrap scheme.

For splitting screens I prefer iTerm2 where you can press ⌘D to split vertically. So my workflow looks like this:

  1. Open a terminal session
  2. Open a file with vim or mvim
  3. Press ⌘D
  4. Execute slimy irb
  5. Press ⌘[ to go back to the Vim buffer
  6. Profit

Automating that further is an exercise for the reader (trivial via Alfred).


Nine One-Liners for Ruby

(over 2 years ago)

I just thought this was an interesting post on Scala showcasing the ease that exists in some higher-level languages for dealing with complex operations on ordered data.

Multiply each item in a list by 2

(1..10).map { |n| n * 2 }

Magic.

Sum a list of numbers

(1..1000).inject(&:+)
(1..1000).inject { |r, n| r += n }
(1..1000).inject(0) { |r, n| r += n }

This takes advantage of Symbol#to_proc, which converts a symbol into a procedure. The above three lines -- I prefer the first -- are semantically equivalent.

Verify if exists in a string

word_list = ["scala", "akka", "play framework", "sbt", "typesafe"]
tweet = "This is an example tweet talking about scala and sbt."
word_list.any? { |w| tweet.include?(w) }  # nice
!(tweet.split & word_list).empty?         # cute, but too cute by half

The latter certainly isn't that efficient, but it's nice to know.

Read in a file

file_text  = File.read("data.txt")
file_lines = File.read("data.txt").lines

The String#lines method applies to strings, not files, although there are other methods to work through lines of files. Either way, working with strings is one of the easier things in Ruby. Many other languages simplify this greatly, too, but I can speak of the pain of doing these things with stuff like VBA and AppleScript.

Happy Birthday to You!

4.times { |n| puts "Happy Birthday #{n == 2 ? "dear NAME" : "to you"}" }

Not too difficult, although it does showcase the legibility of Ruby's string interpolation.

Filter list of numbers

passed, failed = [49, 58, 76, 82, 88, 90].partition { |n| n > 60 }

This will split a list of grades into passing and failing camps. Without such functionality, you'd likely go about this by initializing a passing and failing array, then iterating over the grades and pushing them to the passing and failing arrays based on the outcome of an if statement. That's not horrible, but it's a common enough operation where a bit of terseness is nice.

Fetch and parse an XML web service

require "nokogiri"
require "open-uri"
results = Nokogiri::XML(open("http://search.twitter.com/search.atom?&q=ruby"))

Okay, so this one's not so straightforward as Ruby has two requirements. But Nokogiri is a very nice thing. OpenURI monkey patches the open method to also open URIs.

Find minimum (or maximum) in a list

[14, 35, -7, 46, 98].min
[14, 35, -7, 46, 98].max

The nice thing here is that any object that can implements Enumerable and can compare its elements with each other benefits from min and max.

Sieve of Eratosthenes

(a = (2..127).to_a).each { |n| a.reject! { |m| m > n && m % n == 0 } }

That's hideous. It's not what Ruby's for, and it's not as "exciting" as following through the recursion of Lisp-y languages. But it works.

The other one listed involved parallel processing. You got me there.


Recent Earthquakes in Japan

(over 3 years ago)

Since Japan has been experiencing increased earthquake activity since the Tōhoku earthquake and tsunami on March 11th, I put together the chart below for each daily earthquake (blue dots) and the weekly average (red line) based on data from the JMA. The chart shows daily earthquakes registering at 1.0 or higher on the moment magnitude scale.

Recent quakes

Weekly average is the total magnitude for the week, with the date plotted as the midpoint. I simply add all the seismic moments (M_0 in the formula below) for the week, divide by 7 and convert that to MMS. The scale is logarithmic, such that one quake registering a 9.0 releases 1000 times as much energy one measuring 7.0 does, which likewise releases 1000 times as much energy as one registering 5.0 does. It's not a perfect measure, but it does provide some perspective on directionality.

M_w = 2 / 3 * log(M_0) - 10.7

Unfortunately, the chart I made is missing data for early April, but it's otherwise pretty complete.

You can download the spreadsheet here.

Sources (in Japanese)